Hello,
I've written this script to upload files to my server, and if the file being uploaded is an image file, a thumbnail is automatically created along with it.
But right now, after you select the file, and hit upload, you are brought to a blank screen. I check the directories, and the files are not uploaded. Everything LOOKS right, I just don't know what's wrong.
Here is the url to the script: http://www.chapfreelancer.com/main/temp/adminstration/upload.php
My dirs are chmodded 777.
Here's the source, I added alot of commentary in an attempt to help trouble shoot the problem myself:
<?
// longest side of initial thumbnail
$cvsz = "150";
// height of final thumbnail
$thheight = "100";
// width of final thumbnail
$thwidth = "100";
// get type of uploaded file
$filetype = $_FILES['userfile']['type'];
// define path to upload dir
define("uploadpath", "/home/chapfree/public_html/main/temp/uploads/");
// define path to image upload dir
define("imageuploadpath", "/home/chapfree/public_html/main/temp/uploads/pictures/");
// type that counts as images
$allowed_types = array("image/x-png","image/pjpeg","image/jpeg");
// do this if the form was submitted and a file was uploaded
if($submit AND $file)
{
// do this if file uploaded is an image
if (in_array($filetype,$allowed_types)) {
// I have no idea what this does...
if ($file !="") {
// the following snipet checks if the file exists already on the server,
// if it does, append a number after the file name.
// continues to increment number until a new file name is found
// if someone would be so nice as to comment on what's being done...
// because I did not wirte this snipet (I found it in the manual)
// =============================================================
$FileName = $FILES['userfile']['name'];
while (file_exists(imageuploadpath.$FileName)) {
$SeperatedFileName = explode(".",$FileName);
$FileNameNumIndex = 0;
$FileNameNum = 0;
while (is_numeric(substr($SeperatedFileName[0],-($FileNameNumIndex+1))))
$FileNameNumIndex++;
if ($FileNameNumIndex != 0) {
$FileNameNum = substr($SeperatedFileName[0],-$FileNameNumIndex);
$SeperatedFileName[0] = substr($SeperatedFileName[0], 0,
(strlen($SeperatedFileName[0])-(strlen(strval($FileNameNum)))));
}
$NextFileNameNum = $FileNameNum + 1;
$SeperatedFileName[0] = $SeperatedFileName[0] .
"$NextFileNameNum";
$FileName = implode(".",$SeperatedFileName);
}
copy($FILES['userfile']['tmp_name'], imageuploadpath.$FileName)
or die("The file could not be copied");
// =============================================================
// creates a new image from the file uploaded
$im = imagecreatefromjpeg ("imageuploadpath . $FileName");
// current height and width of $im
$imageheight = imagesy($im);
$imagewidth = imagesx($im);
// determine the conversion factor to make initial thumbnail
if ($height > $imagewidth) {
$cv = $cvsz / $imagewidth; }
else {
$cv = $cvsz / $imageheight; }
// apply the conversion factor to the height and width
$tempthwidth = $cv $imagewidth;
$tempthheight = $cv $imageheight;
// create a new image based on the newly converted dimensions
$tempth = ImageCreate($tempthwidth,$tempthheight);
// create a new scaled down image of the oringinal
ImageCopyResized($tempth,$im,0,0,0,0,$tempthwidth,$tempthheight,$imagewidth,$imageheight);
// determine starting point to take sample
if ($height > $imagewidth) {
$a = $tempthheight - $thheight;
$sch = $a / 2;
$scw = "0"; }
else {
$a = $tempthwidth - $thwidth;
$scw = $a / 2;
$sch = "0"; }
// create a new image with the final thumbnail dimensions
$tempth2 = ImageCreate($thwidth,$thheight);
// take a sample from the initial thumb to be copied into the final thumbnail
imagecopyresampled($tempth2,$tempth,0,0,$scw,$sch,$thwidth,$thheight,$tempthwidth,$tempthheight);
// start defining the necessary info to insert in MySQL and to save image
$filename = $FILES[$tempth2]["name"];
$filetype = $FILES[$tempth2]["type"];
$filetemp = $FILES[$tempth2]["tmp_name"];
$filesize = $FILES[$tempth2]["size"];
// split the file name into just the NAME, and EXTENSION
list($filenameth, $exten) = split ('.', $filename);
// save the final thumbnail image into the server
copy("$tempth2", imageuploadpath . $filenameth . "_thumb" . $exten) or die ("The Thumb could not be created");
//clean up everything
ImageDestroy($tempth);
ImageDestroy($tempth2);
ImageDestroy($im);
}
}
// do this if file is not an allowed image type (simply upload the file)
else { move_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'], uploadpath);
}
}
// print out the form to upload the file
else {
print "<form enctype=\"multipart/form-data\" action=\"" . $PHP_SELF . "\" method=\"post\">";
?>
<input type="hidden" name="MAX_FILE_SIZE" value="500000">
<table width=\"100%\">
<tr>
<td><b>Find the file:</b></td>
<td><input type="file" name="file" size="30">
<td></tr>
<tr>
<td><b>Image Name:</b></td>
<td><input type="text" name="imagename" size="50"></td>
</tr><tr>
<td><b>Description:</b></td>
<td><textarea name="description" cols="50"></text></td>
</tr>
<tr><td></td><td><input type="submit" name="submit" value="Submit!"></td>
</table></form>
<? } ?>
I will be adding the appropriate MySQL queries after I get the upload part to work, which will appearantly be the easiest part.
This will be my first complete successful script if I ever get it to work. Please help, thank you for you time.
Chap