FILES upload

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

lubber123
Forum Commoner
Posts: 51
Joined: Mon Sep 15, 2008 12:26 pm
Location: Manassas, VA

FILES upload

Post by lubber123 »

Can someone point me to a resource where I can read up on file types. I have an upload utility that allows files of type ["type"] == "image/pjpeg" and ["type"] == "image/jpeg". The code uploads some jpg files but not others - I have a catch in the code that says "this isthe wrong file type." Eventhough the images say .jpg this error is caught and it says they are the wrong file type.

So I assume I should find out why they are not being uploaded.

Any suggestions, comments, resources I can go to would be appreciated.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: FILES upload

Post by Darhazer »

The file type in the $_FILES array is provided by the browser, and it's not reliable. If you want to allow only images to be uploaded, use getimagesize() - it will return false if the file is not an image.
lubber123
Forum Commoner
Posts: 51
Joined: Mon Sep 15, 2008 12:26 pm
Location: Manassas, VA

Re: FILES upload

Post by lubber123 »

Oh great. Thanks for the info. I will implement.
lubber123
Forum Commoner
Posts: 51
Joined: Mon Sep 15, 2008 12:26 pm
Location: Manassas, VA

Re: FILES upload

Post by lubber123 »

Sorry to bother you. This is what I am experiencing. In order to get the file to the processing page I have to use the input of file (in order to pick the file.) This means that when it is posted to the processing page it is an $_FILES[''].

I implode the array and extract the part of the string that is the file name - file.jpeg. I found some of the .jpg images that would not be uploaded did not specify their file type. I assume this is what you meant by $_FILES not being accurate in their file types.

Could you guide me as to how to eliminate the $_FILES altogether - or provide me with a reference or tuitorial on how to do this.

Thanks.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: FILES upload

Post by Darhazer »

http://www.php.net/manual/en/features.f ... method.php

I don't know what you are trying to do with the $_FILES array, but you'd hardly need it. Actually I am preprocessing the array only when I have multiple file uploads with the same field name, since the array is in non-trivial form in that case.

So, first you have to check if $_FILES['<your-field-name>']['error'] is UPLOAD_ERR_OK. In any other case the upload have failed.
Then you have to check $result = getimagesize($_FILES['<your-field-name>']['tmp_name']);
if $result == false - it's not an image.

Last, you can move your $_FILES['<your-field-name>']['tmp_name'] to a permanent location. Use basename($_FILES['<your-field-name>']['name']) to check what was the original name of the file. Never trust neither file extension, nor the $_FILES['<your-field-name>']['type'] to check if it is an image.

there are other ways to check MIME type, but if all you need is to know whether it is an image or not, getimagesize() is enought
lubber123
Forum Commoner
Posts: 51
Joined: Mon Sep 15, 2008 12:26 pm
Location: Manassas, VA

Re: FILES upload

Post by lubber123 »

Excellent I wll implement tomorrow. This sounds right. Thanks for your time and effort.
lubber123
Forum Commoner
Posts: 51
Joined: Mon Sep 15, 2008 12:26 pm
Location: Manassas, VA

Re: FILES upload

Post by lubber123 »

Okay, so I have implemented the code as suggested and it works, however, it still does not upload a whole lot of files which are jpegs. I tried to even write some trick code to see if it would upload the images based purely on if the extension is ".jpg" and it recognizes this but still does not upload that file.

I have provided the code below. Perhaps you can see what I am missing. Thanks again.

Code: Select all

 
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);   
$last = substr($_FILES['uploadedfile']['name'], -4, 4); 
                
if(($_FILES['uploadedfile']['error'] == UPLOAD_ERR_OK) || ($last == '.jpg')) {  
        if($result = getimagesize($_FILES['uploadedfile']['tmp_name'])){
        if($_FILES["uploadedfile"]["size"] > 3000000){  ///FILE SIZE
           $teachermessage = "Sorry, the file is too large.  Please reduce the size and re-upload."; 
           header('LOCATION: mtcStudentPage.php?class=' . trim($_SESSION['year']) . '&teaching=' . trim($_SESSION['class_id']) . '&teachermessage=' . $teachermessage);
           exit;
        }
                                
        //Changing the file name
            $varString = implode(",", $_FILES['uploadedfile']);
            $varFile = substr($varString, 0, (stripos($varString, ",")));       
                            
            //deleting the old file if it exists
                        if(file_exists("backgroundimages/" . $varFile)){
                    unlink("backgroundimages/" . $varFile);
            }
                                
                                
            if ($varString["error"] > 0){
                    echo "Return Code: " . $varString["error"] . "<br />";
            }else{
                            
            //UPLOAD FILE
            move_uploaded_file($_FILES["uploadedfile"]["tmp_name"],
            "backgroundimages/" . $_FILES["uploadedfile"]["name"]);
                                  
            //INSERT INTO DB
            $queryattachment = "UPDATE mtc_users SET backgroundimage = '" . $varFile . "' WHERE id = " . $_SESSION['id'];
            $Resultattachment = mysql_query($queryattachment, $connection) or die("Error" . mysql_error());
            $_SESSION['backgroundimage'] = $varFile;
            }
        }
    }else{
                    
    $message = "Sorry, this is the wrong file type.  Please select another file"; 
    header('LOCATION: uploadimage.php?message=' . $message);
    exit;
}   
 
lubber123
Forum Commoner
Posts: 51
Joined: Mon Sep 15, 2008 12:26 pm
Location: Manassas, VA

Re: FILES upload

Post by lubber123 »

After a little more testing it looks like it is allowing files under a certain size to be uploaded. I have sent my host a message asking to check if there is an upload limit on my site. Perhaps that is why I cannot move some of the images.
lubber123
Forum Commoner
Posts: 51
Joined: Mon Sep 15, 2008 12:26 pm
Location: Manassas, VA

Re: FILES upload

Post by lubber123 »

According to my host the uploads can be as large as 1.6GB - far larger than any image I have tried to upload. So, I guess there is another problem. I am still not sure why the larger images will not be uploaded. Do you have any ideas? Is there anything that you see in the code that could show why they are not allowed to upload?

Thanks again for your assistance.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: FILES upload

Post by Darhazer »

Code: Select all

if($_FILES["uploadedfile"]["size"] > 3000000){  ///FILE SIZE
This limits your files to 2.86 MB.
venkylingutla
Forum Newbie
Posts: 5
Joined: Fri Jan 29, 2010 4:39 am

Re: FILES upload

Post by venkylingutla »

hi..
.. modify this as with ur field names and try this.

$fileName = $_FILES['Filedata']['name'];
$ext=substr($fileName, strrpos($fileName, '.') + 1);
//to check whether image is valid or not.
if(($ext=='jpg')||($ext=='jpeg')||($ext=='png') )
{
//write ur code here to move file.
}
lubber123
Forum Commoner
Posts: 51
Joined: Mon Sep 15, 2008 12:26 pm
Location: Manassas, VA

Re: FILES upload

Post by lubber123 »

Hi, sorry for the delayed response - I was short circuited for a while. I will attempt to implement this code this evening. Thanks again for your assistance.
brentwientjes
Forum Newbie
Posts: 8
Joined: Sat Feb 13, 2010 3:29 am

Re: FILES upload

Post by brentwientjes »

I have had lots of problems understanding how to upload files over the past 2 years and and recently figured out why. I posted the reults in PHP Doc under imagecreatefromjpeg and move_upload_file. (http://us2.php.net/manual/en/function.m ... d-file.php) Here is a portion of the note.

The secret sauce is:

1. adjust server memory size, file upload size, and post size
2. convert image to standard formate (in this case jpg) and scale

The server may be adjusted with the .htaccess file or inline code. This example has an .htaccess file with file upload size and post size and then inline code for dynamic system memory.

htaccess file:
php_value post_max_size 16M
php_value upload_max_filesize 6M

PHP code:
ini_set('memory_limit', '100M'); // handle large images

Hope this helps.
lubber123
Forum Commoner
Posts: 51
Joined: Mon Sep 15, 2008 12:26 pm
Location: Manassas, VA

Re: FILES upload

Post by lubber123 »

Okay, thanks I am going to review it tonight. This has been more than a little trying so i appreciate all the help I can get.
Alex94
Forum Newbie
Posts: 1
Joined: Fri Feb 19, 2010 1:02 am

Re: FILES upload

Post by Alex94 »

Hey, I also have a problem with the uploading file. Here my upload_file.php code:

Code: Select all

<?php
if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  echo "Type: " . $_FILES["file"]["type"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }
?> <?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Stored in: " . "upload/" . $_FILES["file"]["tmp_name"];
    }
  }
else
  {
  echo "Invalid file";
  }
?> <?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
|| ($_FILES["file"]["type"] == "image/png")))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
 
    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>
... the HTML form upload.php:

Code: Select all

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"/>
<br />
<input type="submit" name="submit" value="Submit" />
</form>
And here what actually happens: http://doughboys.pytalhost.com/upload.php. I'm not sure if it's because of the path the pictures, or files would be stored in, so please check it, and let me know. I'd appreciate it! :)
Post Reply