tags:

views:

144

answers:

5

Hi - Please can someone help? I have the following code which uploads a file to my server and renames it to whoever the logged in user is. For example the user 'coca-cola-lover' uploads a jpeg - the script would also rename the jpeg 'coca-cola-lover.jpg'.

My problem is that I need it to limit the upload to just jpegs - and also limit the file size to 2mb.

Please help - I was trying to find a solution all night.

Thanks in advance

    // Your file name you are uploading
$file_name = $HTTP_POST_FILES['ufile']['name'];
$username = $row_Recordset1['username'];

$ext = end(explode('.', $file_name));

$renamed_file_name = $username;

$new_file_name=$renamed_file_name.'.'.$ext;

//set where you want to store files
//in this example we keep file in folder upload
//$new_file_name = new upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path= "../sites/images/users/".$new_file_name;
if($ufile !=none)
{
if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
{
echo "Successful<BR/>";

//$new_file_name = new file name
//$HTTP_POST_FILES['ufile']['size'] = file size
//$HTTP_POST_FILES['ufile']['type'] = type of file
echo "File Name :".$new_file_name."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size']."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type']."<BR/>";
}
else
{
echo "Error";
}
}
+2  A: 

getimagesize tells you what format the file is in

as per bgy's comment, you should also force the file extension to be what you want:

 $new_file_name=$renamed_file_name.'.'.$ext; // wrong, uses data from the client

 $new_file_name=$renamed_file_name.'.jpg';   // ok, just what we want

never trust and never use filenames provided by the client.

stereofrog
+1 the best way to determine the file type.
Pekka
You can't rely on getimagesize() because if you inject some php code into a binary image and rename it to 3vil.php you'll be able to execute the embed php code.
Boris Guéry
yeah, right, post updated
stereofrog
OK thanks for the advice. I guess this is in addition to restricting what can be uploaded.
Craig Eves
+1  A: 

You restrict the size via the normal mechanisms, but you'll need to use the fileinfo functions to determine the filetype after uploading.

Ignacio Vazquez-Abrams
+2  A: 

I would recommend exif_imagetype:

<?php
    if (exif_imagetype('image.gif') != IMAGETYPE_GIF) {
        die(The picture is not a gif');
    }

For details see here: http://php.net/manual/en/function.exif-imagetype.php

aefxx
So where would I put this in my code? I have tried but it just returns an error. Thanks in advance
Craig Eves
You will have to make sure that the **exif extension** is enabled in php.ini or built in during compilation. Use `phpinfo()` to check for its existence.
aefxx
+2  A: 

You can use any of the four to detect a mimetype of the file:

You can also limit the MimeType from the FileUpload element, but since this is client-side code, it can easily be removed by malicious users (and it's also buggy across browsers):

<input type="file" name="picture" id="picture" accept="image/jpeg"/>

For further information on how to handle file uploads with PHP (including limiting file size), check the manual.

There is also a lot of very similar questions on Stack Overflow already, one being:

Gordon
+1  A: 

A few advices for the current code

  1. Use $_FILES instead of $HTTP_POST_FILES.
  2. If you need to get file extensions use $extension = pathinfo($filename, PATHINFO_EXTENSION);.
  3. Use is_uploaded_file and move_uploaded_file.
  4. Don't relay on $_FILES['file']['type'] - it can be modified by user.
  5. Indent your code.

If you want to limit file upload to the following requirements:

  1. Filesize: max 2mb.
  2. File type: image/jpeg

Do something like that:

$tmpName = $_FILES['file']['tmp_name'];
if (file_is_uploaded($tmpName) {
    $filesize = fielsize($tmpName);
    $mimeType = exif_imagetype('image.gif');

    if ($filesize <= 2 * 1024 * 1024 && $mimeType == IMAGETYPE_JPEG) {
         $filename = $USERNAME . '.jpg';
         if (move_uploaded_file($tmpName, $filename) == false)  {
             // sth goes wrong
         }
    } else {
         die('Invalid.');
    }
}
Crozin
This looks good - but not sure how to adapt my original code to rename as the username and where it should upload to. Are you able to help further? Thanks in advance
Craig Eves
I've edited my code.
Crozin
thanks again. i'm still lost. i'm new to php and mysql so i'm determined to get this to work. I have added '$username = $row_Recordset1['username'];' to the code you wrote but am not sure where to set the upload path '../sites/images/users/'
Craig Eves
`$filename` - this should be the path where uploaded file will be stored on your server. So if you need to save it in `../sites/images/users/` add this path before `$username`.
Crozin
brilliant. thanks i'll give that a whirl whem i'm at home tonight. thanks again for your help. it's very much appreciated.
Craig Eves
also - how do you add code when writing comments here on stack overflow?
Craig Eves
The same way as in the answers: http://stackoverflow.com/editing-help (Code Spans section).
Crozin