Jump to content

Recommended Posts

I would like to have more of a understanding of this upload file script if any one would like to be a pal and explain whats going on step by step, thats if it's not to much to ask of course :P.

 

<?php
                /* checking the file extension*/
                if ((($_FILES["file"]["type"] == "image/gif")
                || ($_FILES["file"]["type"] == "image/jpeg")
                || ($_FILES["file"]["type"] == "image/png")))
{
                /* this code will be executed if the file extension matches they above (gif,jpeg,png)*\
  		if ($_FILES["file"]["error"] > 0) /*if more than 0 errors occur the following code will be executed*/ 
                                    {
                                      /* return code (string), $_FILES...(file var)*/
                                          echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
   				 }
  else  {
                   /*listing file details, name, file type, file size*/
                  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
                  echo "Type: " . $_FILES["file"]["type"] . "<br />";
                  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
                  /*temp file location*/
                  echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
                  if (file_exists("upload/" . $_FILES["file"]["name"]))
                                {
                                       /* if there is a file already existing  with the same name as the current file in a procedure then the following code will be executed*/
                                       echo $_FILES["file"]["name"] . " already exists. ";
                         }
  else    {
                  /*moving the successfully uploaded file to the a designated location, in this case upload/.*/
                  move_uploaded_file($_FILES["file"]["tmp_name"],
                  "upload/" . $_FILES["file"]["name"]);
                  echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
             }
       }
  }
else   {
          /*this section refers from the first if statement, if the file extension didn’t match the files listed at the beginning of this script then the following code will be executed.*/
             echo "Invalid file extention”
}
?>



i dont get this "error" does it mean an error during the uload

echo "Return Code: " . $_FILES["file"]["error"] . "<br />";

 

/1024 ???

echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";

 

other than these i think i get it

i dont get this "error" does it mean an error during the uload

echo "Return Code: " . $_FILES["file"]["error"] . "<br />";

 

$_FILES["file"]["error"] will contain an error code if their is a problem when uploading the file. All possible error codes are stated in the manual here

http://php.net/manual/en/features.file-upload.errors.php

 

/1024 ???

echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";

 

$_FILES["file"]["size"] stores the files size of the uploaded file. The file size is measured in bytes.

This operation here

$_FILES["file"]["size"] / 1024

Is dividing (thats what the / stands for) the uploaded files file size by 1024 to convert the file size from bytes to kilobytes (There are 1024 bytes in a kilobyte (KB))

 

For more information on how PHP handles file uploads have read of the documentation here

http://php.net/manual/en/features.file-upload.php

If you have trouble understanding your code or someone else's consider re-factoring the code:

 

if(upload_exists($_FILES['file'], 'directory')) {
    echo 'Upload already exists.';
} else if(!upload_is_valid($_FILES['file'])) {
    echo 'Invalid upload.';
} else if(!upload_store($_FILES['file'])) {
    echo 'Could not store the file.';
} else {
    echo 'File successfully created.';
}

function upload_is_valid($upload, $valid_mime, $valid_ext) {
    if($upload['error'] != UPLOAD_ERR_OK)
        return false;
  
    $finfo = finfo_open(FILEINFO_MIME_TYPE); // Note: PHP 5.3.x
    if(!in_array(finfo_file($finfo, $upload['tmp_name']), (array) $valid_mime))
        return false;
    
    if(!in_array(pathinfo($upload['tmp_name'], PATHINFO_EXTENSION), (array) $valid_ext))
        return false;
    
     return true;
}

function upload_exists($upload, $directory) {
    $filename = pathinfo($upload['tmp_name'], PATHINFO_FILENAME);
    return file_exists(rtrim($directory, '\/') . DIRECTORY_SEPARATOR . $filename);
}

function upload_store($upload, $directory) {
    $upload = upload_create($upload);
    $filename = pathinfo($upload['tmp_name'], PATHINFO_FILENAME);
    return move_uploaded_file(rtrim($directory, '\/') . DIRECTORY_SEPARATOR . $filename);
}

function upload_create($upload) {
    // re-create the image
    return $upload;
}

So these 9 lines are harder to comprehend then your 30-40 lines of code?

 

if(upload_exists($_FILES['file'], 'directory')) {
    echo 'Upload already exists.';
} else if(!upload_is_valid($_FILES['file'])) {
    echo 'Invalid upload.';
} else if(!upload_store($_FILES['file'])) {
    echo 'Could not store the file.';
} else {
    echo 'File successfully created.';
}

I was actualy refering to...

function upload_is_valid($upload, $valid_mime, $valid_ext) {    if($upload['error'] != UPLOAD_ERR_OK)        return false;      $finfo = finfo_open(FILEINFO_MIME_TYPE); // Note: PHP 5.3.x    if(!in_array(finfo_file($finfo, $upload['tmp_name']), (array) $valid_mime))        return false;        if(!in_array(pathinfo($upload['tmp_name'], PATHINFO_EXTENSION), (array) $valid_ext))        return false;         return true;}function upload_exists($upload, $directory) {    $filename = pathinfo($upload['tmp_name'], PATHINFO_FILENAME);    return file_exists(rtrim($directory, '\/') . DIRECTORY_SEPARATOR . $filename);}function upload_store($upload, $directory) {    $upload = upload_create($upload);    $filename = pathinfo($upload['tmp_name'], PATHINFO_FILENAME);    return move_uploaded_file(rtrim($directory, '\/') . DIRECTORY_SEPARATOR . $filename);}function upload_create($upload) {    // re-create the image    return $upload;}

This part even...[

function upload_is_valid($upload, $valid_mime, $valid_ext) {    if($upload['error'] != UPLOAD_ERR_OK)        return false;      $finfo = finfo_open(FILEINFO_MIME_TYPE); // Note: PHP 5.3.x    if(!in_array(finfo_file($finfo, $upload['tmp_name']), (array) $valid_mime))        return false;        if(!in_array(pathinfo($upload['tmp_name'], PATHINFO_EXTENSION), (array) $valid_ext))        return false;         return true;}function upload_exists($upload, $directory) {    $filename = pathinfo($upload['tmp_name'], PATHINFO_FILENAME);    return file_exists(rtrim($directory, '\/') . DIRECTORY_SEPARATOR . $filename);}function upload_store($upload, $directory) {    $upload = upload_create($upload);    $filename = pathinfo($upload['tmp_name'], PATHINFO_FILENAME);    return move_uploaded_file(rtrim($directory, '\/') . DIRECTORY_SEPARATOR . $filename);}function upload_create($upload) {    // re-create the image    return $upload;

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.