Safe Upload Examples Output:

PHP5 is not capable of addressing files with multi-byte characters in their names at all (including Arabic language).



Verified Conditions:

  1. Max uploaded file size is 1 MB
  2. Accepted MIME Content-types are: image/jpeg, image/gif, image/png, and image/svg+xml

Safe Upload Examples Code:

<?php

if(isset($_POST['submit'])){

    require '../../Arabic.php';
    $Arabic = new I18N_Arabic('Transliteration');
    
    // Continue only if the file was uploaded via HTTP POST
    if (is_uploaded_file($_FILES['image']['tmp_name'])) {

        // Is file size less than 1 MB = 1,048,576 Byte
        if ($_FILES['image']['size'] < 1048576) {

            // Detect MIME Content-type for a file 
            $mime = mime_content_type($_FILES['image']['tmp_name']);
            
            // List of accepted MIME Content-type
            $images = array('image/jpeg', 'image/gif', 'image/png', 'image/svg+xml');

            if (in_array($mime, $images)) {

                // PHP5 is not capable of addressing files with multi-byte characters in their names at all.
                // This is why we use Transliteration functionality in Arabic class
                $filename = trim($Arabic->ar2en($_FILES['image']['name']));
                
                // Moves an uploaded file to a new location
                move_uploaded_file ($_FILES['image']['tmp_name'], $dir.DIRECTORY_SEPARATOR.$filename);
            } else {
                echo '<h3>You can upload image file only (i.e. gif, jpg, png, and svg)!</h3>';
            }
        } else {
            echo '<h3>You can not upload file bigger than 1MB!</h3>';
        }
    } else {
        echo '<h3>You have to select file first to upload it!</h3>';
    }
}

?>

<form  action="SafeUploadTransliteration.php" method="post" enctype="multipart/form-data">

    <input name="image" type="file" size="60">

    <input name="submit" type="submit" value="Upload">

</form><br />