Jonathan L
Jonathan L

Reputation: 21

Php file upload uppercase extension doesn't work

I am having a problem uploading files coming from an HTML form. It works only when the extension is not in uppercase. Uploading .jpg will work, but .JPG will not (even after renaming the file on the system). I am completely baffled, I have set no restriction on file extensions since my website is for very personal use. Here is the html form

<form enctype="multipart/form-data" action="uploader.php" method="post">
Browse file : <input type="file" name="img"><br>
Nom : <input type="text" name="nom"><br>
Prix : <input type="text" name="prix"><br>
Description: <input type="text" name="descr"><br>
<input type="submit" value="Upload">
</form>

Here is my script for uploading.

$target_path = "../photos/" . $_FILES['img']['name'];

if(move_uploaded_file($_FILES['img']['tmp_name'], $target_path)){
    code here
}

I will supply further information if nescessary, thanks for helping!

Upvotes: 2

Views: 3053

Answers (2)

Lumis
Lumis

Reputation: 21639

Had the same problem - just lower the case of the target path

$target_path =  $target_dir . strtolower(basename($_FILES["fileToUpload"]["name"]));

Upvotes: 1

user3778731
user3778731

Reputation: 11

I believe this issue concerns the file upload size configuration in your particular php.ini file not the case of the file extension. The file upload module should be case insensitive. The default upload size for WAMP, for example, is set at a max file size of 2M. I had the issue of uppercase file extensions not being loaded as well, when I compared the file size of the files with uppercase extensions to those with lowercase, the uppercase were over 2M and the lower under- I don't know if this is a windows thing where case denotes file size. In any case what happens is that the $_FILES assoc array is populated, in the case of a single file upload, with a single key value pair. The key being the name value you set in the form input and the value an associative array concerning aspects of the file upload with the keys: name, type, size, tmp_name, and error. The upload file module gets as far as populating the file name, but when it encounters the size larger than the config setting the rest of the assoc array is not populated. So, the file is not moved into the configured tmp directory, the type is not set, size is not set, however, error is set to 1. Because the file is never uploaded to the tmp directory you can't extract it and move it. The solution, go to your php.ini file do a search for the text "file upload" and check the max file size for uploads and compare this against your test files, edit your php.ini accordingly.

Upvotes: 1

Related Questions