Forum Moderators: coopster

Message Too Old, No Replies

Very odd issue with file upload script

         

galahad2

5:44 pm on Jan 11, 2010 (gmt 0)

10+ Year Member



I have a strange issue with a PHP image upload script that I'm using to place photos in a specific directory on a site.

Basically, it works for a few images but not most of them. The script only accepts JPG files but beyond that there's no reason why it wouldn't accept some JPGs and not others.

The code is as follows:

[PHP]
<?php
$idir = "../images/carpictures/"; // Path To Images Directory
$tdir = "../images/carpictures/"; // Path To Thumbnails Directory
$twidth = "125"; // Maximum Width For Thumbnail Images
$theight = "100"; // Maximum Height For Thumbnail Images

if (!isset($_GET['subpage'])) { // Image Upload Form Below ?>
<form method="post" action="addnewphotos.php?subpage=upload" enctype="multipart/form-data">
File:<br />
<input type="file" name="imagefile" class="form">
<br /><br />
<input name="submit" type="submit" value="Submit" class="form"> <input type="reset" value="Clear" class="form">
</form>
<? } else if (isset($_GET['subpage']) && $_GET['subpage'] == 'upload') { // Uploading/Resizing Script
$url = $_FILES['imagefile']['name']; // Set $url To Equal The Filename For Later Use
if ($_FILES['imagefile']['type'] == "image/jpg" ¦¦ $_FILES['imagefile']['type'] == "image/jpeg" ¦¦ $_FILES['imagefile']['type'] == "image/pjpeg") {
$file_ext = strrchr($_FILES['imagefile']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php
$copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']); // Move Image From Temporary Location To Permanent Location
if ($copy) { // If The Script Was Able To Copy The Image To It's Permanent Location
print '<p>Image uploaded successfully.</p><br />'; // Was Able To Successfully Upload Image
$simg = imagecreatefromjpeg("$idir" . $url); // Make A New Temporary Image To Create The Thumbanil From
$currwidth = imagesx($simg); // Current Image Width
$currheight = imagesy($simg); // Current Image Height
if ($currheight > $currwidth) { // If Height Is Greater Than Width
$zoom = $twidth / $currheight; // Length Ratio For Width
$newheight = $theight; // Height Is Equal To Max Height
$newwidth = $currwidth * $zoom; // Creates The New Width
} else { // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)
$zoom = $twidth / $currwidth; // Length Ratio For Height
$newwidth = $twidth; // Width Is Equal To Max Width
$newheight = $currheight * $zoom; // Creates The New Height
}
$dimg = imagecreate($newwidth, $newheight); // Make New Image For Thumbnail
imagetruecolortopalette($simg, false, 256); // Create New Color Pallete
$palsize = ImageColorsTotal($simg);
for ($i = 0; $i < $palsize; $i++) { // Counting Colors In The Image
$colors = ImageColorsForIndex($simg, $i); // Number Of Colors Used
ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']); // Tell The Server What Colors This Image Will Use
}
imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It)
imagejpeg($dimg, "$tdir" . $url); // Saving The Image
imagedestroy($simg); // Destroying The Temporary Image
imagedestroy($dimg); // Destroying The Other Temporary Image
print '<p>Image created successfully.</p><p><a href="addnewphotos2.php">Upload another image</a></p><p><a href="admincomplete.php">Click here if finished</a></p>'; // Resize successful
} else {
print '<font color="#FF0000">ERROR: Unable to upload image.</font>'; // Error Message If Upload Failed
}
} else {
print '<font color="#FF0000">ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is '; // Error Message If Filetype Is Wrong
print $file_ext; // Show The Invalid File's Extention
print '.</font>';
}
} ?>
[/PHP]

The script still appears to work (i.e doesn't throw an error) even on the occasions when it fails to upload the JPG file.

With two files found so far it actually works- with the others the file that it tries to upload doesn't appear on the server.

The permissions of all these files are the same. I'm uploading them from a Windows box to a Linux server.

The script has no particular file size limit, and all the files I'm trying to upload are roughly the same size (about 50K).

I've checked other attributes such as (a) making sure the images are all the same image format i.e RGB, and (b) checking to see if the script doesn't like particular resolutions- but there's no rhyme or reason to it.

This one has me stumped- any ideas?

optik

9:03 pm on Jan 11, 2010 (gmt 0)

10+ Year Member



have you checked the file names of the suspect files, will some files always work and others not or is the problem more random.

rocknbil

11:14 pm on Jan 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's only one way to "beat" problems like this, ones that have no specific indicator of what's broken and seem to be random: logging.

Define a log file, make sure it's writable:

$upload_log = '/server/path/to/upload-log.txt';
$log_data = NULL' // Squelches undefined errors when we concatenate

At various points in your script, append to $log_data:

$log_data .= "beginning upload at $dateTime, file $upload_file\n";
///////
$log_data .= "Moving uploaded file\n";
//////
$log_data .= "Resizing file\n";
//////
$log_data .= "$dateTime $upload_file complete\n=======\n\n";

Just before printing the response, append the log file with the data. Along the way, trap every possible error and include it in $log_file.

It's also good to set a $max_log_size variable so it doesn't get too huge. If the size of the log file is > $max_log_size, overwrite it, otherwise, append it.

You may never need it again if it reveals the problem, but then, you never know . . you might.