Okay, this is really weird...
I am working on a greeting cards system and I have a function that adds new cards to a database and uploads the corresponding image in an images/cards/ folder (which is set at 777 so it's not a permissions problem...).
I have an entry in the db with the filename, which is always the card's id plus its extensions (so all cards are named c_id.ext, and all thumbnails are named t_id.ext - example: c_55.jpg, i_55.jpg).
The thing is... last week it was working fine. But now this morning I had a very unpleasant surprise. When I tried to add a new card, or upload a new image for an existing card, the image (both thumbnail and card images) is all screwy. It would add some weird lines, or take the first portion of the image and repeat it but with different hues, etc.
Another really weird thing, is that when on my HD, the image was like 18k, and when on the server, it became 36k! 😕
Note that for a card update, I always delete the current card image first, then reupload a new one (normally copy() overwrites automatically, but if the extension is different - say the old one was a .gif and the new one a .jpg - the old file (.gif) would linger on the server).
I haven't changed the script, and as far as I know, neither the Apache config nor the php.ini have been changed. The only changes to the server since last week was that we solved a qmail problem (nothing to do with it), and that it was down for a while this morning and we rebooted it (but before it went down it was already doing the screwy image problem).
If I upload a file named c_id.ext by FTP and overwrite it, it's fine... doesn't change its size either.
Even though I haven't changed anything at all in my image upload script, I assume the problem is in there (I use the copy() function as stated in the topic title), but what could it be? Is it something in Apache maybe? 😕
Here's my function for the image upload... Note that I have a MAX_FILE_SIZE hidden field so if the image is too large, it will automatically be empty (size=0).
(Incoming variables are the $_FILE["image"] data, the directory where it's uploaded, and the new filename (w/o the extension yet) which is either "c_id" or "i_id":
function imgUpload($file, $dir, $newname)
{
if (is_dir($dir))
{
if ($file["size"]) // Size check
{
$ext = strrchr($file["name"], "."); // Get the file's extension
if ($ext != "" && in_array($ext, $this->EXT)) // Perform extension check
{
$newname .= $ext;
if (!copy($file["tmp_name"], $dir . $newname)) // Copy file to server
{
$this->error_msg("Error in copying file: " . $file["name"]);
return false;
}
else
{
chmod($dir . $newname, 0777);
return $newname; // Return complete file name, with extension
}
}
else
{
$this->error_msg("Error: file extension $ext not supported (file: {$file['name']}).
Please upload a file with one of the following extensions: " . join($this->EXT, ", ") . ".");
return false;
}
}
else
{
$this->error_msg("Error: either you specified a wrong file/path,
or your files were too large or invalid. Please try again.");
return false;
}
}
else
{
$this->error_msg("Fatal error: directory $dir not found!");
return false;
}
}
Note that I ALSO tried to use PHP's FTP functions to upload the picture, instead of the copy() function, and the images were STILL corrupt. 🙁 It works fine if I upload them via a regular FTP client like FlashFXP or CuteFTP.
Also, this happens on any computer, not just this one, so it's not my computer or my browser or anything.
WTF?
I am using PHP 4.2.2.
If anyone has a clue as to what might be causing this, I'd be eternally grateful. Thanks!