Wrox Programmer Forums
|
BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6
This is the forum to discuss the Wrox book Beginning PHP, Apache, MySQLWeb Development by Michael K. Glass, Yann Le Scouarnec, Elizabeth Naramore, Gary Mailer, Jeremy Stolz, Jason Gerner; ISBN: 9780764557446
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 9th, 2004, 12:29 PM
Registered User
 
Join Date: May 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default bestand uploaden

hello,

I'm having a problem with making a upload script.

My pictures go to the right directory, but when they are there you can't open them.

This is my code for selecting the picture:

<html>
<head>
  <title>Administration - upload new files</title>
</head>
<body>
<h1>Upload new news files</h1>
<form enctype="multipart/form-data" action="upload.php" method="post">
  <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
  Upload this file: <input name="userfile" type="file">
  <input type="submit" value="Send File">
</form>
</body>
</html>

This is my code for uploading and showing the picture.

<html>
<head>
  <title>Uploading...</title>
</head>
<body>
<h1>Uploading file...</h1>
<?php

  // $userfile is where file went on webserver
  $userfile = $HTTP_POST_FILES['userfile']['tmp_name'];

  // $userfile_name is original file name
  $userfile_name = $HTTP_POST_FILES['userfile']['name'];

  // $userfile_size is size in bytes
  $userfile_size = $HTTP_POST_FILES['userfile']['size'];

  // $userfile_type is mime type e.g. image/gif
  $userfile_type = $HTTP_POST_FILES['userfile']['type'];

  // $userfile_error is any error encountered
  $userfile_error = $HTTP_POST_FILES['userfile']['error'];
// userfile_error was introduced at PHP 4.2.0
// use this code with newer versions
  if ($userfile_error > 0)
  {
    echo 'Problem: ';
    switch ($userfile_error)
    {
      case 1: echo 'File exceeded upload_max_filesize'; break;
      case 2: echo 'File exceeded max_file_size'; break;
      case 3: echo 'File only partially uploaded'; break;
      case 4: echo 'No file uploaded'; break;
    }
    exit;
  }
// end of code for 4.2.0

// put the file where we'd like it
  $upfile = 'C:\uploads/'.$userfile_name;

// is_uploaded_file and move_uploaded_file added at version 4.0.3
  if (is_uploaded_file($userfile))
  {
     if (!move_uploaded_file($userfile, $upfile))
     {
        echo 'Problem: Could not move file to destination directory';
        exit;
     }
  }
  else
  {
    echo 'Problem: Possible file upload attack. Filename: '.$userfile_name;
    exit;
  }

  echo 'File uploaded successfully<br /><br />';

// reformat the file contents
  $fp = fopen($upfile, 'r');
  $contents = fread ($fp, filesize ($upfile));
  fclose ($fp);

  $contents = strip_tags($contents);
  $fp = fopen($upfile, 'w');
  fwrite($fp, $contents);
  fclose($fp);
// show what was uploaded
  echo 'Preview of uploaded file contents:<br />';
?>
<img src="file:///C:\uploads/<?php echo $userfile_name ?>">
<?php
  echo '<br />';

?>
</body>
</html>

I will be a verry happy man if somebody could help me.

 
Old June 16th, 2004, 04:43 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

What, if any, errors are you seeing? Do you have error_reporting activated and set to E_ALL?

A few things to point out:

 $upfile = 'C:\uploads/'.$userfile_name;

Your slashes are inconsistent here. Both should be backslashes. Most PHP'ers write Windows paths like this:

'C:\\uploads\\'.$userfile_name;

But I don't think the slashes /have/ to be escaped.

Why do you move the file, open the file and then write the file again? Any operations that need to be preformed on the file can be done all at once before its moved to the destination directory.

I wrote a couple of upload tutorials a while back, have a read of those here:
http://p2p.wrox.com/topic.asp?TOPIC_ID=12104
http://p2p.wrox.com/topic.asp?TOPIC_ID=11883

Also does the file appear in the destination directory on upload? You may also have a problem with permissions. I wrote a tutorial for that as well...
http://p2p.wrox.com/topic.asp?TOPIC_ID=11962

Better to reference this via HTTP instead of the local file system.
file:///C:\uploads/<?php echo $userfile_name ?>

Let us know how it works out!

HTH!

Regards,
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.