SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast wyte raven's Avatar
    Join Date
    Feb 2007
    Location
    England, High Wycombe Bucks
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Whats the difference?

    Hello Everyone!

    Firstly, can I say a thankyou to Sunit21, without him, I'd have never have got this problem resolved:

    http://www.sitepoint.com/forums/showthread.php?t=462990

    But the question I want to ask...whats the difference between the two code examples below? Why isn't the original archived (copy & pasted) code doing the intended result? Why is it generating an error? The original code (from the archives of chapter 10 Kevin Yanks book) is as follows:

    PHP Code:
    if (!is_uploaded_file($_FILES['upload']['tmp_name'])) {
        exit(
    'There was no file uploaded!');
      }
      
    $uploadfile $_FILES['upload']['tmp_name'];
      
    $uploadname $_FILES['upload']['name'];
      
    $uploadtype $_FILES['upload']['type'];
      
    $uploaddesc $_POST['desc'];
          
      
    // Open file for binary reading ('rb')
      
    $tempfile fopen($uploadfile'rb');

      
    // Read the entire file into memory using PHP's
      // filesize function to get the file size.
      
    $filedata fread($tempfilefilesize($uploadfile));

      
    // Prepare for database insert by adding backslashes
      // before special characters.
      
    $filedata addslashes($filedata); 
    And after putting this code in, I would get the error generated (see link above).

    However, after a few alterations:

    PHP Code:
    if (!is_uploaded_file($_FILES['upload']['tmp_name'])) {
        exit(
    'There was no file uploaded!');
      }
      
    $uploadfile $_FILES['upload']['tmp_name'];
      
    $uploadname $_FILES['upload']['name'];
      
    $uploadtype $_FILES['upload']['type'];

      
    // ------------This is a new line--------
      
    $uploadsize $_FILES['upload']['size'];

      
    $uploaddesc $_POST['desc'];
          
      
    // Open file for binary reading ('rb')
      
    $tempfile fopen($uploadfile'rb');

      
    // ---------Altered this line also ----------
      
      
    $filedata fread($tempfile$uploadsize);

      
    // Prepare for database insert by adding backslashes
      // before special characters.
      
    $filedata addslashes($filedata); 
    The code works as it's intended.

    Can somebody please tell me why the archived script (very top) doesn't work, and why the modified script (below) does?. I get the impression they are both doing exactly the same thing. Have I missed something out on this line:

    $filedata = fread($tempfile, filesize($uploadfile));

    I'm still learning, and so if anyone can offer an explanation to help me understand I'd very much appreciate it.
    Wyte R@ven - Creator of the Rift

  2. #2
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually in the first is filesize($uploadfile) is wrong because it is not pointing to the uploaded file.

    PHP Code:
    $uploadfile $_FILES['upload']['tmp_name']; //String with the file name
    filesize($uploadfile// = 0 
    So it actually read zero bytes from the file.

  3. #3
    SitePoint Enthusiast wyte raven's Avatar
    Join Date
    Feb 2007
    Location
    England, High Wycombe Bucks
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hiya

    Can you give me an example, using the script I have supplied (very top) how I would use the filesize() function? I ask this because, both the book and archive explains this method, and I have had to go around it, without using the filesize function.
    Wyte R@ven - Creator of the Rift

  4. #4
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I myself would use the file_get_contents function to get the filedata.

    Code:
    $filedata = file_get_contents($path_to_the_file); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •