Register
It is currently Thu Feb 22, 2018 12:40 am

[Resolved] A PHProblem


All times are UTC


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Wed Jul 08, 2009 7:21 pm   
Member
User avatar

Joined: Thu Jan 08, 2004 1:05 pm
Posts: 245
Location: Las Vegas, Nevada, U.S.A.
So I'm playing with PHP and I've gotten stuck when it comes to uploading files to the server (which is localhost in this case). I've developed a simple script to eliminate as many variables as I can in the hopes of narrowing down the problem. The code looks like this:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <title>file upload test</title>
</head>
<body>
<form name="form" enctype="multipart/form-data" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" />
   <input type="hidden" name="MAX_FILE_SIZE" value="32768" />
   <input type="file" id="file" name="file" />
   <input type="submit" name="submit" />
</form>
<?php
if(isset($_POST['submit'])) {
   echo "File Name: " . $_FILES['file']['name'] . '<br>' .
   "File Type: " . $_FILES['file']['type'] . '<br>' .
   "File Size: " . $_FILES['file']['size'] . '<br>' .
   "Temp Name: " . $_FILES['file']['tmp_name'] . '<br>' .
   "ErrorCode: " . $_FILES['file']['error'];
}
?>
</body>
</html>


To make things easy to post here, I've done my browsing in lynx. Firefox gives the same behaviour, so I don't believe that the problem is browser related.

In lynx, page originally displays like this:

Quote:
____________________ Submit


I enter a file name in the space provided (say /home/myusername/Pictures/54.png) and page displays like:

Quote:
____________________ Submit
File Name: 54.png
File Type: image/png
File Size: 438
Temp Name: /tmp/phpdb7xei
ErrorCode: 0


But, look for the file in /tmp

Code:
ls -lta


and no new file is there. The time-stamp for /tmp has been updated though.

The directives in the 'File Uploads' section of /etc/php5/apache2/php.ini are set as follows:

Code:
;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;

; Whether to allow HTTP file uploads.
file_uploads = On

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
;upload_tmp_dir =

; Maximum allowed size for uploaded files.
upload_max_filesize = 2M


I think the above directives are OK for this test.

No doubt I've overlooked something incredibly simple. Please point out what a fool I am so that I can go on with my learning.

Thanks for looking.

_________________
I've just picked up a fault in the AE35 unit. It's going to go 100% failure in 72 hours.


Last edited by d_riordan on Thu Jul 09, 2009 1:35 pm, edited 1 time in total.

Top
 Profile WWW  
 PostPosted: Thu Jul 09, 2009 1:08 am   
Ultimate Member
User avatar

Joined: Sun Feb 15, 2004 12:46 am
Posts: 2200
Location: St-Jean sur Richelieu, Quebec
Nowhere in your code are you moving the uploaded file to your uploads folder....


Top
 Profile WWW  
 PostPosted: Thu Jul 09, 2009 1:09 am   
Ultimate Member
User avatar

Joined: Sun Feb 15, 2004 12:46 am
Posts: 2200
Location: St-Jean sur Richelieu, Quebec
check here for an example:

http://www.tizag.com/phpT/fileupload.php


Top
 Profile WWW  
 PostPosted: Thu Jul 09, 2009 1:35 pm   
Member
User avatar

Joined: Thu Jan 08, 2004 1:05 pm
Posts: 245
Location: Las Vegas, Nevada, U.S.A.
Ok, I'm a fool.

I had been using the 'move_uploaded_file()' function previously, but had been getting errors like:
Quote:
Warning: move_uploaded_file(images/54.png) [function.move-uploaded-file]: failed to open stream: Permission denied in /var/www/php/upload.php on line 28
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpamVyIZ' to 'images/54.png' in /var/www/php/upload.php on line 28

So, I omitted the function in an effort to simplify the code. I was under the impression that an uploaded file would be written to /tmp and left there to deal with later, and began focusing on that 'issue'.

I was also under the impression that I had set the permissions on the target directory to 777. Oops! I was wrong about that.

Using the move_uploaded_file() function with the proper permissions on the target directory is working just fine.

I still have one question though. No file ever appeared in /tmp. Is the file being uploaded as it should, then deleted as soon as the code is finished executing? That would explain the updated timestamp on /tmp AND the fact that no new file existed there, but my reading had suggested that the file might stay there for a while.

As I originally suspected, it turned out to be something very simple. Thanks for making me take a second look.

_________________
I've just picked up a fault in the AE35 unit. It's going to go 100% failure in 72 hours.


Top
 Profile WWW  
 PostPosted: Mon Jul 20, 2009 1:33 am   
Ultimate Member
User avatar

Joined: Sun Feb 15, 2004 12:46 am
Posts: 2200
Location: St-Jean sur Richelieu, Quebec
TMP is only a temporary directory.
Nothing is supposed to actually stay there, it's just a transition area.
It acts like anything stored in your RAM or your SWAP.


Top
 Profile WWW  
 PostPosted: Tue Jul 21, 2009 1:14 pm   
Member
User avatar

Joined: Thu Jan 08, 2004 1:05 pm
Posts: 245
Location: Las Vegas, Nevada, U.S.A.
I knew tmp was temporary, but I'd always envisioned it as 'short term parking'. Now I see it's often more like 'curb side drop off'.

Thanks for the help.

_________________
I've just picked up a fault in the AE35 unit. It's going to go 100% failure in 72 hours.


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


Powered by phpBB © 2012 phpBB Group
© 2003 - 2012 USA LINUX USERS GROUP