In need of help

  • Socno
  • Graduate
  • Graduate
  • Socno
  • Posts: 107
  • Loc: New York

Post 3+ Months Ago

Hi. I am watching some online tutorials on php on youtube. This one tutorial is about uploading files. I have tried to redo his example, but get an error on the last lines. Anyone able to see whats wrong with the code? Thanks!

Code: [ Select ]
<?php

$name = $_FILES['file']['name'];
$extension = strtolower(substr($name, strpos($name, '.') +1));
$type = $_FILES['file']['type'];

//$size = $_FILES['file']['size'];

$tmp_name = $_FILES['file']['tmp_name'];

if (isset($name)) {
 if (!empty($name)) {
  
 if (($extension=='jpg'||$extension=='jpeg')&&$type=='image/jpeg') {

  $location ='uploads/';
  
  if (move_uploaded_file($tmp_name, $location.$name)) {
   echo 'Uploaded!';
 } else {
   echo 'There was an error';
 }

 } else {
   echo 'File must be a jpg/jpeg';
 }
 
 } else {
   echo 'Please chooose a file.';
 }

?>

<form action="upload.php" method="POST" enctype="multipart/form-data">
   <input type="file" name="file"><br><br>
   <input type="submit" value="Submit">
   </form>
  1. <?php
  2. $name = $_FILES['file']['name'];
  3. $extension = strtolower(substr($name, strpos($name, '.') +1));
  4. $type = $_FILES['file']['type'];
  5. //$size = $_FILES['file']['size'];
  6. $tmp_name = $_FILES['file']['tmp_name'];
  7. if (isset($name)) {
  8.  if (!empty($name)) {
  9.   
  10.  if (($extension=='jpg'||$extension=='jpeg')&&$type=='image/jpeg') {
  11.   $location ='uploads/';
  12.   
  13.   if (move_uploaded_file($tmp_name, $location.$name)) {
  14.    echo 'Uploaded!';
  15.  } else {
  16.    echo 'There was an error';
  17.  }
  18.  } else {
  19.    echo 'File must be a jpg/jpeg';
  20.  }
  21.  
  22.  } else {
  23.    echo 'Please chooose a file.';
  24.  }
  25. ?>
  26. <form action="upload.php" method="POST" enctype="multipart/form-data">
  27.    <input type="file" name="file"><br><br>
  28.    <input type="submit" value="Submit">
  29.    </form>
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

It would help if you included the error you're seeing.
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 3+ Months Ago

You were missing the last } in the script

PHP Code: [ Select ]
<?php
 
$name = $_FILES['file']['name'];
$extension = strtolower(substr($name, strpos($name, '.') +1));
$type = $_FILES['file']['type'];
 
//$size = $_FILES['file']['size'];
 
$tmp_name = $_FILES['file']['tmp_name'];
 
if (isset($name)) {
   
    if (!empty($name)) {
 
        if (($extension == 'jpg' || $extension=='jpeg') && $type == 'image/jpeg') {
 
            $location ='uploads/';
 
            if (move_uploaded_file($tmp_name, $location.$name)) {
                echo 'Uploaded!';
            }  else {
                echo 'There was an error';
            }
 
        }  else {
            echo 'File must be a jpg/jpeg';
        }
 
    }  else {
        echo 'Please chooose a file.';
    }
   
} // <-- This little guy was missing
 
?>
 
<form action="upload.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="file"><br><br>
    <input type="submit" value="Submit">
</form>
 
 
  1. <?php
  2.  
  3. $name = $_FILES['file']['name'];
  4. $extension = strtolower(substr($name, strpos($name, '.') +1));
  5. $type = $_FILES['file']['type'];
  6.  
  7. //$size = $_FILES['file']['size'];
  8.  
  9. $tmp_name = $_FILES['file']['tmp_name'];
  10.  
  11. if (isset($name)) {
  12.    
  13.     if (!empty($name)) {
  14.  
  15.         if (($extension == 'jpg' || $extension=='jpeg') && $type == 'image/jpeg') {
  16.  
  17.             $location ='uploads/';
  18.  
  19.             if (move_uploaded_file($tmp_name, $location.$name)) {
  20.                 echo 'Uploaded!';
  21.             }  else {
  22.                 echo 'There was an error';
  23.             }
  24.  
  25.         }  else {
  26.             echo 'File must be a jpg/jpeg';
  27.         }
  28.  
  29.     }  else {
  30.         echo 'Please chooose a file.';
  31.     }
  32.    
  33. } // <-- This little guy was missing
  34.  
  35. ?>
  36.  
  37. <form action="upload.php" method="POST" enctype="multipart/form-data">
  38.     <input type="file" name="file"><br><br>
  39.     <input type="submit" value="Submit">
  40. </form>
  41.  
  42.  

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 121 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.