Page 1 of 1

uploading files on a unix machine results in empty files

Posted: Thu Feb 26, 2004 6:02 am
by kylebragger
hi, i'm new around here and i've got a question. this is a snippet of code from a download manager i'm working on for a client. this part of the code handles a file upload. the filename is a randomly generated uid with the appropriate file extension:

Code: Select all

$filename = 'files/' . md5(rand()) . '.' . get_file_extension($_FILESї'the_file']ї'name']);
if (move_uploaded_file($_FILESї'the_file']ї'tmp_name'],$filename))
the files acutally do get uploaded to the /files/ directory fine; the problem, however, is that the files are 0kb and empty inside; images are broken, text files are empty, html files contain no code, etc. what could the issue or issues be?

Posted: Thu Feb 26, 2004 6:09 am
by patrikG
You'll have to divulge more code, especially the function move_uploaded_file().

Posted: Thu Feb 26, 2004 6:18 am
by kylebragger
move_uploaded_file() is a built in php function; i don't have source code for it. that is all the code that is relevant to the problem. here is part of my form's html:

Code: Select all

<form name="dataform" action="admin.php" method="POST" enctype="multipart/form-data">
	<input type="hidden" name="page" value="<?php echo $page; ?>" />
	<input type="hidden" name="id" value="<?php echo $file_id; ?>" />
	
	<p><label for="file_name">File Name:</label><br />
	<input type="text" name="file_name" id="file_name" value="<?php echo $file_name; ?>" size="50" /></p>
	
	<?php if (!$file_okay) echo '<p class="msg">Since this is a new download, you must upload a file.</p>'; ?>
	<p><label for="file">File:</label><br />
	<input type="file" name="the_file" id="file" /><br />
thanks

Posted: Thu Feb 26, 2004 6:26 am
by patrikG
Without the function, I can't help you.
I suppose it's a built-in Dreamweaver function?

Posted: Thu Feb 26, 2004 6:43 am
by kylebragger
move_uploaded_file() is a function included in the core php functionality. it copies a file from the temporary upload directory specified in php.ini to a filename you supply:

[php_man]move_uploaded_file[/php_man]

from php manual: bool move_uploaded_file ( string filename, string destination)


This function checks to ensure that the file designated by filename is a valid upload file (meaning that it was uploaded via PHP's HTTP POST upload mechanism). If the file is valid, it will be moved to the filename given by destination.

If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE.

If filename is a valid upload file, but cannot be moved for some reason, no action will occur, and move_uploaded_file() will return FALSE. Additionally, a warning will be issued.

This sort of check is especially important if there is any chance that anything done with uploaded files could reveal their contents to the user, or even to other users on the same system.

Note: When safe mode is enabled, PHP checks whether the files or directories you are about to operate on have the same UID (owner) as the script that is being executed.

Note: move_uploaded_file() is not affected by the normal safe mode UID-restrictions. This is not unsafe because move_uploaded_file() only operates on files uploaded via PHP.


Warning
If the destination file already exists, it will be overwritten.

Posted: Thu Feb 26, 2004 7:35 am
by patrikG
Oh, that's nice - one of PHP's commands I've never used in over four years. :lol:

I usually use [php_man]copy[/php_man] and [php_man]unlink[/php_man] to handle file-uploads.

I can't see anything particularly wrong with your code - you obviously have chmod the upload directory "files" to 0755 - otherwise there wouldn't be even empty files in your directory.

Posted: Thu Feb 26, 2004 12:19 pm
by kylebragger
hmm could it be some sort of server issue?

Posted: Thu Feb 26, 2004 12:39 pm
by kylebragger
problem solved

ohk, the problem has been solved. the funniest part is, i didn't do anything. i got home from school and poof! everything worked. :?: strange indeed.

thanks for the insight.

Posted: Thu Feb 26, 2004 2:00 pm
by patrikG
Rather painless solution - excellent :)

Posted: Thu Feb 26, 2004 2:02 pm
by malcolmboston
lol, thats happened to me many times, unfortunately the other way round

it is working, leave the room and it breaks

weird seeing as i live alone 8O