Page 1 of 1
Bizarre uploading problem
Posted: Sat May 17, 2003 9:49 pm
by uberpolak
I'm using this script to upload files (the $path value comes from a form):
Code: Select all
<?php
$fp = fopen($path, 'r');
$data = fread($fp, filesize($path));
fclose($fp);
$nf = fopen('images/' . basename($path), 'w');
fputs($nf, $data);
fclose($nf);
?>
The file is created fine, but only some of it is actually written. I tried this with two different 12kb files, for one it took 915 bytes and for the other 566 bytes. Since these are images I wind up with corrupted image files. My upload_max_filesize in php.ini is set to 2M, which should be more than enough room for a 12kb file, shouldn't it?. I'm extremely confused by this.
Posted: Sat May 17, 2003 9:59 pm
by evilcoder
I would recommend using something like this:
Create a file, and place this code in it... this is where the form for the uploading file will be.
Code: Select all
<form enctype="multipart/form-data" action="upload.php" method="post">
<input type="hidden" name="max_file_size" value="2000000">
<tr>
<td><b>file upload</b></td>
<td></td>
</tr>
<tr>
<td>file: </td>
<td><input type="File" name="userfile" size="30" maxlength="255">
</td>
</tr>
<tr>
<td>target filename: </td>
<td><input type="Text" name="newname" size="30" maxlength="^255">
</td>
</tr>
<tr>
<tr>
<td>upload diretory: </td>
<td>c:\apache\upload\</td>
</tr>
<td colspan="2" align="CENTER">
<INPUT TYPE="submit" VALUE="upload">
</td>
</tr>
</form>
Then create a file called upload.php, and put this in it.
Code: Select all
<?php
function do_upload( $filename , $newname )
{
$file = basename( $filename );
$tmp_upload_path = "/tmp/";
$new_file_name = "directory/".$newname; // Relative to www.your.com/
if ( !copy( $tmp_upload_path.$file, $new_file_name ) ) echo "failed to copy file<br>\n";
return;
}
?>
<html>
<head>
<title>uploading</title>
</head>
<body text="#000000">
<?php
do_upload( $userfile , $newname );
?>
<table>
<tr>
<td><b>upload report</b></td><td></td>
</tr>
<tr>
<td>upload tmp file:</td><td><?php echo $userfile; ?></td>
</tr>
<tr>
<td>file name:</td><td><?php echo $userfile_name; ?></td>
</tr>
<tr>
<td>target file name:</td><td><?php echo $newname; ?></td>
</tr>
<tr>
<td>file size:</td><td><?php echo $userfile_size; ?></td>
</tr>
<tr>
<td>file type:</td><td><?php echo $userfile_type; ?></td>
</tr>
</table>
</body>
</html>
Hiope that helps
Posted: Sat May 17, 2003 10:08 pm
by uberpolak
Thanks, I tweaked the copy function to work with the site, that helps plenty. Wow, a function to do exactly what I wanted and I didn't even know it existed...
Posted: Sat May 17, 2003 10:25 pm
by evilcoder
It didnt.... It was just created.
