Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    May 2007
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    File Upload will not work! No error, yet no upload?

    Hello. I am pretty new at php, and for this project I am actually just moving a website from it's old Apache server to our new Windows server. I've gotten PHP setup on the server, the mySQL database is setup and working correctly. This seems to be the last issue.

    Here is my code:

    PHP Code:
    // if $_FILES['thefile'] isn't empty, try to copy the file
    if ($_FILES['thefile'] != "") {

    $myfile $_FILES['thefile']['name'];

    if (
    is_uploaded_file($myfile)) { 
             print 
    "Uploaded file found"
              
    // move the file here 
             
    move_uploaded_file($_FILES['thefile']['tmp_name'], "../Access/files/".$_FILES['thefile']['name'])
                     or die(
    "<b>Couldn't move the file!</b>");
          } else { 
             print 
    "Error Code: ".$_FILES['thefile']['error']." <br>File: ".$_FILES['thefile']['name']." <br>Temp: ".$_FILES['thefile']['tmp_name']; 
          } 
    } else {

             
    // if $_FILES['thefile'] was empty, die and let us know why
             
    die("No input file specified");

    I always get the following output when trying to upload the file:

    "Error Code: 0
    File: Bylaws of.pdf
    Temp: E:\temp\phpUploads\php83F3.tmp"

    There is never any files in my temp directory, nor in the destination directory.

    What am I doing wrong? This code was used on the old server with no problems. I changed the temp directory and destination directory, as well as set "Everyone" with full Permissions in both folders on the server (just to be sure there wasn't a permissions issue).

    What is going on here? Can someone shed some light?

    Thanks!

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,018
    Thanks
    2
    Thanked 313 Times in 305 Posts
    The temporary files are deleted by php when the script ends, so unless you are fast or you deliberately introduce a delay in the script's execution, you will never see the files in the temp folder.

    is_uploaded_file() only works on the ['temp'] element, so the posted code will always execute the code in the "else" branch of the logic.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    New Coder
    Join Date
    May 2007
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Ah...

    Well that makes sense that it would test is_uploaded_file against the temp file.....I guess I wasn't thinking there.

    So I changed the is_uploaded_file to test for the tmp_name of the file, and alas it did show "Uploaded file found." But then it had the message: "Couldn't move the file!", which is the error when move_uploaded_file() fails.

    Any ideas why this might be happening? I feel like I am so close now. Thanks again.

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,018
    Thanks
    2
    Thanked 313 Times in 305 Posts
    Add the following two lines of code immediately after your first opening <?php tag -
    PHP Code:
    ini_set ("display_errors""1");
    error_reporting(E_ALL); 
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    New Coder
    Join Date
    May 2007
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok I think I've got the problem figured out, but I still need a little more direction.

    I just had the realization that the Destination path was probably relative to the temporary directory, not the directory that my php file was in. (My .php file was in "siteroot/admin/do_mod_bylaws.php" and I wanted the file to upload to "siteroot/access/files/"). So I changed my temp directory (in php.ini) to "siteroot/temp/", so that my relative file path ("../access/files/") would work. And it did!

    So now my question: I currently have the permissions for "Everyone" set to "Full Control" for both the temp directory and the Access/files directory. I know this is not the best/safest way to achieve write permissions. Is there a specific user that php uses (similar to "Network Service/NT Authority" that ASP.net uses), that way I don't have to give full control to these directories to Everyone?

    In other words, what do I need to do (and for which users) with my directory permissions to allow file uploads?

    Thanks!


  •  

    Posting Permissions

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