Jump to content

HTML/PHP/JavaScript help - image size and extension form validation


beemer832

Recommended Posts

I am working on a PHP project that will allow a user to upload a handful of images, and then submit text to go along with it.

 

To simplify the uploading process of a file, I restricted the type of file that can be uploaded (jpg) and the size (2.5mb). The problem is, the form is calling to a PHP script, and I have error logging built in to alert the user of why a file didn't upload, but since the PHP script is just being called, no errors are being displayed on the page. I guess unless I do a refresh of a few seconds to view any errors on the page....

 

What I would like to do is show a pop-up/error message notifying the user that the file type or size is incorrect and needs to be adjusted. Now part of the problem is, the file needs to be uploaded to the server first, and then everything can be checked, so I am really getting confused.

 

If someone can point me in the right direction on what I could use to accomplish this, it would be greatly appreciated. Thanks

 

-beemer

Link to comment
Share on other sites

This is the code that uploading the file, checking for size and file type, and then displaying errors on the page.

 

Thanks for moving the thread, didn't know where it needed to be posted.

 

<?
header("location: /classifieds/index.php");

echo '<html><center>';

//first lets upload any files that were selected//
$date = date("m/d/y",time());

//check that we have a file
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
  
  //Check if the file is JPEG image and it's size is less than 350Kb
  $filename = basename($_FILES['uploaded_file']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && 
    ($_FILES["uploaded_file"]["size"] < 2500000)) {
    
    //Determine the path to which we want to save this file
      $newname = dirname(__FILE__).'/uploads/'.$filename;
      
      //Check if the file with the same name is already exists on the server
      if (!file_exists($newname)) {
        
        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
            //strip off file path for $newname variable so path is not accessible via html//
            $tempnewname = explode('/', $newname);
            echo $tempnewname;
            $newname=$tempnewname[9].'/'.$tempnewname[10];
           echo "It's done! The file has been saved as: ".$filename;
        } else {
           echo "Error: A problem occurred during file upload!";
        }
      } else {
         //echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";//
         $timestampname = str_replace('.jpg', date('j-n-Y_g:i:s').'.jpg', (basename($_FILES['uploaded_file']['name'])));
         $path = dirname(__FILE__).'/uploads/';
         $fullname = $path.$timestampname;
         
            
            //strip off file path for $newname variable so path is not accessible via html//
            $tempnewname = explode('/', $fullname);
            $newname=$tempnewname[7].'/'.$tempnewname[8];
            $picname=$tempnewname[8];
            
            
         ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname)));
      }
    } else {
        echo "Error: Only .jpg images under 2.5MB are accepted for upload";
           }
}

else {
echo "Error: No file uploaded";
}
?>

 

 

Link to comment
Share on other sites

Great question....

 

I have included the entire PHP page below, I was leaving out the DB information and some image manipulation code. I am redirecting the page back to the main page, index.php as there is no data on this page. Is this incorrect?

 

<?
header("location: /classifieds/index.php");

echo '<html><center>';

//first lets upload any files that were selected//
$date = date("m/d/y",time());

//check that we have a file
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
  
  //Check if the file is JPEG image and it's size is less than 350Kb
  $filename = basename($_FILES['uploaded_file']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && 
    ($_FILES["uploaded_file"]["size"] < 2500000)) {
    
    //Determine the path to which we want to save this file
      $newname = dirname(__FILE__).'/uploads/'.$filename;
      
      //Check if the file with the same name is already exists on the server
      if (!file_exists($newname)) {
        
        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
            //strip off file path for $newname variable so path is not accessible via html//
            $tempnewname = explode('/', $newname);
            echo $tempnewname;
            $newname=$tempnewname[9].'/'.$tempnewname[10];
           echo "It's done! The file has been saved as: ".$filename;
        } else {
           echo "Error: A problem occurred during file upload!";
        }
      } else {
         //echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";//
         $timestampname = str_replace('.jpg', date('j-n-Y_g:i:s').'.jpg', (basename($_FILES['uploaded_file']['name'])));
         $path = dirname(__FILE__).'/uploads/';
         $fullname = $path.$timestampname;
         
            
            //strip off file path for $newname variable so path is not accessible via html//
            $tempnewname = explode('/', $fullname);
            $newname=$tempnewname[7].'/'.$tempnewname[8];
            $picname=$tempnewname[8];
            
            
         ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname)));
      }
    } else {
        echo "Error: Only .jpg images under 2.5MB are accepted for upload";
           }
}

else {
echo "Error: No file uploaded";
}
?>

<?
//Convert full size image to 1-thumbnail size 2-similar size for all images. files will be saved in corresponding folders//

include('SimpleImage.php');
   $image = new SimpleImage();
   $image->load($fullname);
   $image->resize(175, 115);
   $image->save("images/thumb/2$picname");
   $thumbpic="images/thumb/2$picname";
   
   $image = new SimpleImage();
   $image->load($fullname);
   $image->resize(640, 480);
   $image->save("images/full/2$picname");
   $fullpic="images/full/2$picname";
   
?>



<?
//now lets enter this information into the DB//

include'dbconnect.php';

$title=$_POST['title'];
$description=$_POST['description'];
//$images=$_POST['uploaded_file'];//
$price=$_POST['price'];

mysql_connect($hostname,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO parts VALUES
('','$title','$description','$thumbpic','$fullpic','$price')";

mysql_query($query);

mysql_close();
?>
</html>

 

thanks

-Beemer

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.