SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry upload script not working

    ive got an upload script that i want to use in my script. it supposed to enable the admin side to upload an image, to the images folder. but it doesnt seem to work. the rest of the form however adds the information to the db. it keeps showing me this error. "There was an error uploading the file, please try again!".

    PHP Code:
     <?php
    if (!isset($_POST['submit'])) {
    ?>
    </p>
    <p align="center"><strong>Add DVDs </strong></p>
    <form action="" method="post" enctype="multipart/form-data">
      <p><strong>Product ID: 
        </strong><br>
        <input type="text" name="productid">
      </p>
      <p><strong>Product Name: </strong><br>
        <input type="text" name="product_name">
      </p>
      <p><strong>Product Description: </strong><br>
        <textarea  name="product_desc" rows="6" cols="75" wrap="hard"></textarea>
      </p>
      <p>
      <strong>Product Price:</strong><br /> 
        <input type="text" name="product_price">
      </p>
      <p><strong>Category ID:</strong><br />
        <select name="catid"  size="1"> 
          <br>
          <option value="1" selected="selected"> Family
          <option value="2"> Comedy
          <option value="3"> Sci-Fi
        <option value="4"> Action
        <option value="5"> Thriller
        </select>
      </p>
      <p><strong>Quantity:</strong><br/>
        <input type="text" name="quantity">
    </p>
     <p><strong>Director:</strong><br/>
        <input type="text" name="director">
     <p><strong>Actors:</strong><br/>
        <input type="text" name="actors">
     <p><strong>Certificate:</strong><br/>
        <input type="text" name="certificate">
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    Choose a file to upload: <input name="uploadedfile" type="file" /><br />
      <p align="left">
        <input type="submit" name="submit" value="Submit!"> &nbsp;&nbsp; <input type="reset" value="Reset">
      </p>


    </form>
    <?php

    } else {
    // Where the file is going to be placed 
    $target_path "images/";

    /* Add the original filename to our target path. Result is "uploads/filename.extension" */
    $target_path $target_path basename$_FILES['uploadedfile']['name']); 

    // This is how we will get the temporary file...
    $_FILES['uploadedfile']['tmp_name']; 

    $target_path "images/";

    $target_path $target_path basename$_FILES['uploadedfile']['name']); 

    if(
    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
        echo 
    "The file "basename$_FILES['uploadedfile']['name']). " has been uploaded";
    } else{
        echo 
    "There was an error uploading the file, please try again!";


    $productid $_POST['productid'];
    $product_name $_POST['product_name'];
    $product_desc $_POST['product_desc'];
    $product_price $_POST['product_price'];
    $catid $_POST['catid'];
    $quantity $_POST['quantity'];
    $director $_POST['director'];
    $actors $_POST['actors'];
    $certificate $_POST['certificate'];

    //mysql_query("INSERT INTO products (productid, product_name, product_desc, product_price, catid, product_image, quantity) 

    //VALUES ('', '$product_name', '$product_desc', '$product_price', '$catid', '$quantity', '$director', '$actors', '$certificate')");
    $query "INSERT INTO products VALUES ('$productid','$product_name', '$product_desc', '$product_price', '$catid', '$quantity', 
    '
    $director', '$actors', '$certificate')";
     
    $result mysql_query($query)
        or die(
    mysql_error());
    echo 
    $query;
    echo 
    "Success! Your product has been added!";
    }
    ?>
    Last edited by vish; Apr 25, 2006 at 06:27. Reason: forgot to enclose in php tags

  2. #2
    SitePoint Evangelist Waffles's Avatar
    Join Date
    Nov 2005
    Posts
    435
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you CHMOD'ed the upload folder (set it's permissions to 777)?

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im using this on localhost at the current time. but if i move the upload script in its own file, and create a sepearet html file, it works and uploads the file. but when i insert the script in this file it doesnt work.

  4. #4
    SitePoint Evangelist Waffles's Avatar
    Join Date
    Nov 2005
    Posts
    435
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are using localhost, right click the upload folder and check it isn't set to "read only".

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cheers it worked, do you if it is possible to rename the file b4 it is uploaded, because i would like to make it the same as the productid

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    $target_path just before the move_uploaded_file() call contains the destination filename for saving your upload. Modify that to change the filename.

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks its worked, i just need to mess around with it to meet my other objective, if i have problems i will be back

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ive changed it so the file that file being uploaded is now renamed to the name of the productid. but i dnt want to have a deafult extension of .jpeg. i need it to be any file extension is that possible. thanks for ne help

    $target_path = $target_path . ($_POST['productid'].'.jpeg');

  9. #9
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Find the last period in the filename and get the text that appears after it, that's the extension. There are many ways to do this and I'm sure you can handle it, you've been on the PHP forum a while now


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •