Is this file image upload safe? Is this image uploading script safe? I have spent my day researching this and it would be very much appreciated if you could point out any errors or ways to make it safer.

&lt;?php

$max_file_size = 1048576; // expressed in bytes
                          // 10240 = 10 KB
                          // 102400 = 100 KB
                          // 1048576 = 1 MB
                          // 10485760 = 10 MB

$upload_errors = array(
  // http://www.php.net/manual/en/features.file-upload.errors.php
  UPLOAD_ERR_OK         =&gt; "No Errors",
  UPLOAD_ERR_INI_SIZE   =&gt; "Larger than upload_max_filesize",
  UPLOAD_ERR_FORM_SIZE  =&gt; "Larger than form MAX_FILE_SIZE",
  UPLOAD_ERR_PARTIAL    =&gt; "Partial upload",
  UPLOAD_ERR_NO_FILE    =&gt; "No file",
  UPLOAD_ERR_NO_TMP_DIR =&gt; "No temporary directory",
  UPLOAD_ERR_CANT_WRITE =&gt; "Can't write to disk",
  UPLOAD_ERR_EXTENSION  =&gt; "File upload stopped by extension"
  );

$image_types = array (
  IMAGETYPE_GIF         =&gt; "gif",
  IMAGETYPE_JPEG        =&gt; "jpg",
  IMAGETYPE_PNG         =&gt; "png"
  );


$image_whitelist = array(IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF);

$message = "";
if (isset($_POST['submit'])) {

  // This would store the actual user id
  $userId = 1;

  // store uploaded file's temp path.
  $tmp_file = $_FILES['file_upload']['tmp_name'];

  if($_FILES['file_upload']['error'] &gt; 0){
    $error = $_FILES['file_upload']['error'];
    die($upload_errors[$error]);
  }

  // check to see if file is an image, if not die.
  if(!getimagesize($tmp_file)) {
    die('Please ensure you are uploading an image.');
  }

  // check to see if image is a valid type, if not die.
  if (!in_array(exif_imagetype($tmp_file), $image_whitelist)) {
    die('Please upload a valid image. (.jpg, .png or .gif)');
  }

  $target_file = md5($userId . date('U')) . "." . $image_types[exif_imagetype($tmp_file)];
  $upload_dir = "uploaded";

  // check to see if file already exsits if it does die.
  if (file_exists($upload_dir."/".$target_file)) {
    die('file already exists');
  }

  //move_uploaded file will return false if $tmp_file is not a valid upload file
  // or if it cannot be moved for any other reason
  if (move_uploaded_file($tmp_file, $upload_dir."/".$target_file)) {
    $message = "File uploaded successfully.";
  } else {
    die("move_uploaded_file() error.");
  }
}
?&gt;

&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;File Upload&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;?php if(!empty($message)) { echo "&lt;p&gt;{$message}&lt;/p&gt;"; } ?&gt;
  &lt;form action="fileupload.php" enctype="multipart/form-data" method="POST"&gt;
    &lt;input type="hidden" name="MAX_FILE_SIZE" value="&lt;?php echo $max_file_size; ?&gt;"&gt;
    &lt;input type="file" name="file_upload"&gt;
    &lt;input type="submit" name="submit" value="Upload"&gt;
  &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;


Many Thanks
It is pretty solid for saving images on a single web server. Some optional/extra things you could do are:


Host the images on a CDN. That way if there was any PHP or JS injected in, it would not be able to access any cookies or directories on the web server you use to serve content.
Your code would block out PHP files in disguise from being uploaded because of the getimagesize() and the exif_imagetype(). You can add this bit to an htaccess file in the images directory as well to disable PHP files from being executed on their own. This wouldn't stop the file from being executed though if your code automatically included the "image" in the code somewhere.

&lt;IfModule mod_php5.c&gt;
php_flag engine off
&lt;/IfModule&gt;