Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-19-2008, 08:54 PM   PM User | #1
Punkcrib
New Coder

 
Join Date: May 2007
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Punkcrib is an unknown quantity at this point
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!
Punkcrib is offline   Reply With Quote
Old 12-19-2008, 09:16 PM   PM User | #2
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,712
Thanks: 2
Thanked 251 Times in 243 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
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.
CFMaBiSmAd is online now   Reply With Quote
Old 12-19-2008, 09:30 PM   PM User | #3
Punkcrib
New Coder

 
Join Date: May 2007
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Punkcrib is an unknown quantity at this point
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.
Punkcrib is offline   Reply With Quote
Old 12-19-2008, 09:49 PM   PM User | #4
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,712
Thanks: 2
Thanked 251 Times in 243 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
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.
CFMaBiSmAd is online now   Reply With Quote
Old 12-19-2008, 10:07 PM   PM User | #5
Punkcrib
New Coder

 
Join Date: May 2007
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Punkcrib is an unknown quantity at this point
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!
Punkcrib is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:21 AM.


Advertisement
Log in to turn off these ads.