Bizarre uploading problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

Bizarre uploading problem

Post 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.
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post 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 ) 
&#123;
  $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;
&#125;
?>

<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
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

Post 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...
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

It didnt.... It was just created. :)
Post Reply