Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Image Upload 1

Status
Not open for further replies.

TheSponge

Technical User
Jul 2, 2003
442
GB
Hello

My PHP image upload script works great! however not if the user is using firefox?

any ideas please?

thanks

A+,CCNA
 
<?php


// Create the main upload function
function do_upload() {

// Create an array containing all valid upload file types for this script
$allowed_types = array(
"image/gif" => "gif",
"image/pjpeg" => "jpg",
"image/jpg" => "jpg",
// Add more types here if you like
);

// Check to see if the file type is in the allowed types array
if(!array_key_exists($_FILES['userfile']['type'], $allowed_types)) {
die("Invalid file type.");
}

// Set the maximum uploadable file size => 512000 = 500kb
$maxfilesize = 512000;

// Is the file larger than it is allowed to be?
if($_FILES['userfile']['size'] > $maxfilesize) {
die("File too large");
}

// Where will the file be uploaded to?
$uploaddir = $_SERVER['DOCUMENT_ROOT'] . "/uploads/";

// What is the files temporary name?
$file = $_FILES['userfile']['tmp_name'];

// What is the files actual name?
$filename = $_FILES['userfile']['name'];

// Does this file already exist on the server?
if(file_exists($uploaddir . $filename)) {
die("A file with that name already exists on this server.");
} else {
// This file does not already exist, so copy it.
copy($file, $uploaddir.$filename) or die("Could not copy file.");
}

// All done! :)
echo "Upload successful";
echo "<br />";
echo "<br />";
echo "Copy and Paste the following Code!";
echo "<font color=green><b>";
echo "<br />";
echo "http://www.mysite.com/uploads/";
echo $filename;
echo "";
}









?>
<html>
<head>
<title>Image Upload</title>
</head>
<body bgcolor="#dddddd">
<center>
<img src="images/upload.gif">
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="action" value="do_upload">
<input type="file" name="userfile"><br />
<input type="submit" name="submit" value="Upload Image">
</form>
</body>
</html>
<?php
// If the form has been completed, execute the upload function (above).
if($_POST['action'] == "do_upload") {
do_upload();
}

A+,Network +,CCNA
 
Thank you.

I now need greater details on the malfunction of the script when run with FireFox. "works great! however not if the user is using firefox" is a little vague.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
when a user uploads an image from firefox,

it says "invalid file type" even though it was a valid type (jpg)

A+,Network +,CCNA
 
What filetype does FireFox send? I notice your error message only outputs a description of the error, not what the error input was. What happens if FireFox sends image/jpeg"?


Change:

die("Invalid file type.");

to:

die("Invalid file type:" . $_FILES['userfile']['type']);

and see what happens.




Want the best answers? Ask the best questions!

TANSTAAFL!!
 
firefox sends a .jpg, I send the same file from IE and its fine.

A+,Network +,CCNA
 
incidently, a .gif works fine in both IE and Firefox

A+,Network +,CCNA
 
firefox sends a .jpg, I send the same file from IE and its fine.
I assumed that.

incidently, a .gif works fine in both IE and Firefox
That would not suprise me. There is only one accepted spelling for "GIF". But both "JPG" and "JPEG" are accepted.


Do you understand that $_FILE['userfile']['type'] is sent by the browser, not set by PHP? Your debugging technique seem to be suggest otherwise.

Again, what will your code do if FireFox, instead of sending "image/jpg", sends "image/jp[red]e[/red]g"?

Try the simple change I suggested and verify that FireFox is sending the filetype you're expecting.




Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Yep, I was right. On my system, when I take your script, make the change I suggested to it then run it, when I upload an JPG via Firefox, I get:

Invalid file type:image/jpeg

Since "image/jpeg" isn't in your script's list of approved filetypes, the script barfs on the input. Sounds to me like you need to take the advice of the comment in your script:

// Add more types here if you like


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
It worked thankyou very much,

but why if I defined .jpg, would it not except it?

A+,Network +,CCNA
 
You didn't define .jpg. You matched one file type descriptor to the file extension .jpg.


When the browser uploads a file, it is the browser that sends the data your script finds in $_FILES['userfile']['type']. So when IE sends the jpg, it sends a file type of "image/jpg" and FireFox sends a file type "image/jp[red]e[/red]g".

Your script maintains an array, $allowed_types, which uses image types as its indeces. The indeces of that array include "image/gif", "image/pjpeg" and "image/jpg" but not "image/jp[red]e[/red]g".

Your script then performs:

[tt]if(!array_key_exists($_FILES['userfile']['type'], $allowed_types))[/tt]

which checks the file type (sent by the browser) against the indeces of the array $allowed_types. When IE sends "image/jpg", the script finds the index in allowed types and accepts the file. When FireFox sends "image/jp[red]e[/red]g" the script barfs.

Add a new entry to $allowed_types.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top