Jump to content

Recommended Posts

Hi, I have got this script that uploads images.

 

If I put the file types that are allowed to be uploads in this file, then the script works fine.

 

However, I need to call these permitted file types from an external file called definitions.php.  When I run the script it allows .gif files but not anything else. I have been working on this for hours now and am pulling my hair out, so any help is very much appreciated.

 

The script and the included file are below.

 

Script - file_upload.php

 

<?php
include("definitions.php");
if ((($_FILES["file"]["type"] == $file1) ||
($_FILES["file"]["type"] == $file2) ||
($_FILES["file"]["type"] == $file3) ||
($_FILES["file"]["type"] == $file4) ||
($_FILES["file"]["type"] == $file5))
&& ($_FILES["file"]["size"] < 50000))
  {
   $UploadDir = "../../members/uploads/";
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
       if (file_exists($UploadDir . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      $UploadDir . $_FILES["file"]["name"]);
      $image = ($UploadDir . $_FILES["file"]["name"]);
      echo "<img src=$image>"; 
      
      }
    }
    else {
    echo "Invalid File";
    }
    ?>

 

definitions.php

 

<?php
$file1 = "image/jpg";
$file2 = "image/gif";
$file3 = "image/tiff";
$file4 = "image/png";
$file5 = "image/bmp";
?>

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/153386-file-upload-problem/
Share on other sites

Your code is technically correct. It is likely that the other files you are attempting to upload don't match any of the values or their size is greater than 50000 or there is an upload error - http://us.php.net/manual/en/features.file-upload.errors.php

 

That code -

 

1) Should test for upload errors before using any of the uploaded $_FILES information.

2) Should use an array of acceptable types and then use the in_array() function to test if the type is in the array.

3) Should test the type and the size separately and output a different message for each test that fails. The way it is now you have no idea which test, type or size, failed.

4) The error message should display the value that failed so that you have feedback as to why the test failed. Different browsers send different mime types for the same file.

 

Short answer: Your code is not doing anything to tell you why the upload failed, what makes you think someone in a forum can?

Link to comment
https://forums.phpfreaks.com/topic/153386-file-upload-problem/#findComment-805859
Share on other sites

Hi, thanks for your reply. It turned out to be that instead of

 

image/jpg

 

it had to be

 

image/pjpeg

 

Now I am having the same problem with

 

image/png

 

but image/ppng does not work. What is the case here, and why do the standard file names not work in all cases?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/153386-file-upload-problem/#findComment-805887
Share on other sites

jpeg uses

image/jpg
image/jpeg
image/pjpeg

 

png.. uses

image/png
image/x-png

 

 

EDIT:

PS i assume you uploaded via IE.. which used all these mime!

as a tip.. if a file fails just add

echo $_FILES["file"]["type"];

and upload again to get its mime!

Link to comment
https://forums.phpfreaks.com/topic/153386-file-upload-problem/#findComment-805898
Share on other sites

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.