SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict XiledWeb's Avatar
    Join Date
    May 2005
    Location
    Nashville
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Upload Script Problems (Error Messages)

    Hi guys, I'm in the process of writing an upload form and I'm receiving errors that I need help with.

    Here is the code I'm using:

    PHP Code:
    <?
    //file_upload.php
    function upload_form() {
    ?>

    <form method="POST" enctype="multipart/form-data" 
       action="<? echo $PHP_SELF ?>">
       <p><label for="name">Your Name:</label>
       <input type="text" id="name" name="name" size="30" /></p>
       <p><label for="company">Company:</label>
       <input type="text" id="company" name="company" size="40" /></p>
       <p><label for="phone">Phone Number:</label>
       <input type="text" id="phone" name="phone" size="25" /></p>
       <p><label for="email">Email Address:</label>
       <input type="text" id="email" name="email" size="30" /></p>
       <p><label for"comments">Special Comments:</label>
       <textarea id="comments" name="comments" wrap="soft" cols="30" rows="7"></textarea></p>
       <p><input type="hidden" name="action" value="upload">Upload file:</p>
       <input type="file" name="userfile">
       <input type="submit" name="submit" value="Upload">
    </form>

    <?
    }

    function 
    upload_file() {
      
    //set the archive directory
    $archive_dir "path/to/uploads/";
    //get the userfile particulars
    $userfile_name $_FILES['userfile']['name'];
    $userfile_tmp_name $_FILES['userfile']['tmp_name'];
    $userfile_size $_FILES['userfile']['size'];
    $userfile_type $_FILES['userfile']['type'];
    if(isset(
    $_ENV['WINDIR'])) {
      
    $userfile str_replace("\\\\","\\"$_FILES['userfile']['name']);
    }
    $filename basename($userfile_name);
    if(
    $userfile_size <= 0) die ("$filename is empty.");
    if(!@
    move_uploaded_file($userfile_tmp_name"$archive_dir/$filename"))
     die (
    "Can't copy file $userfile_name to $filename.");
    if(isset(
    $_ENV['WINDIR']) && !@unlink($userfile))
     die (
    "Can't delete the file $userfile_name.");
    echo 
    "$filename has been successfully uploaded.<br />";
    echo 
    "Filesize: " number_format($userfile_size) . "<br />";
    echo 
    "Filetype: $userfile_type<br />";
    }  
    ?>
    <html>
    <body>
    <?
    if($action == 'upload'upload_file();

    else 
    upload_form();
    ?>
    The error I'm receiving is "Can't copy file test.doc to test.doc".

  2. #2
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It looks like your problem might be that $archive_dir ends with a "/", and then you stick another between it and $filename in move_uploaded_file.

    Good idea for debugging to turn up error reporting and to not use @ in front of functions, especially those that you know are generating errors!
    PHP questions? RTFM
    MySQL questions? RTFM

  3. #3
    SitePoint Addict XiledWeb's Avatar
    Join Date
    May 2005
    Location
    Nashville
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I removed the '/' from the $archive_dir, but I left in the '/' between the $archive_dir and $filename in move_uploaded_file.

    The error I get now is:

    Warning: move_uploaded_file(/path/to/archive/dir$filename) [function.move-uploaded-file]: failed to open stream: Permission denied in /path/to/archive/test/file_upload.php on line 46

    Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phppZ6pjW' to '/path/to/archive/dir/uploads$filename' in /path/to/archive/dir/file_upload.php on line 46
    Can't copy file test.doc to test.doc.

  4. #4
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Weird, PHP doesn't seem to be expanding your variables. Try putting the '/' back in $archive_dir and then replace your move_uploaded_file with this one:
    PHP Code:
    move_uploaded_file($userfile_tmp_name$archive_dir.$filename
    PHP questions? RTFM
    MySQL questions? RTFM

  5. #5
    SitePoint Addict XiledWeb's Avatar
    Join Date
    May 2005
    Location
    Nashville
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That gave me the same error message as the previous one. Back to the drawing board...

  6. #6
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's really weird that PHP is not expanding the variable $filename...

    Try putting in a line echo $archive_dir.$filename; and see what that spits out for you.
    PHP questions? RTFM
    MySQL questions? RTFM

  7. #7
    SitePoint Addict XiledWeb's Avatar
    Join Date
    May 2005
    Location
    Nashville
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can't copy file test.doc to test.doc.

  8. #8
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by XiledWeb View Post
    Can't copy file test.doc to test.doc.
    That shouldn't be the output of
    echo $archive_dir.$filename;
    PHP questions? RTFM
    MySQL questions? RTFM

  9. #9
    SitePoint Addict XiledWeb's Avatar
    Join Date
    May 2005
    Location
    Nashville
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kromey View Post
    That shouldn't be the output of
    echo $archive_dir.$filename;
    Sorry - the output it gave me was :

    path/to/dir/uploads/test.doc

  10. #10
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, then the next thing to check is to ensure that path/to/dir/uploads/ exists. If it doesn't, then that would be why your script is failing.
    PHP questions? RTFM
    MySQL questions? RTFM


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
  •