Issue with upload script

I have an upload script that is giving me some issues

I uploads blank information into the database and never does move the image to the upload folder

I’m not sure where my mistake is but I’ve look to long with no answer

Upload script…

[php]if($_SERVER[‘REQUEST_METHOD’] == ‘POST’) {

function getExtension($str) {
$i = strrpos($str,’.’);
if (!$i) { return ‘’; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;

$filename = $_FILES['image']['name'];
$tmp_file = $_FILES['image']['tmp_name'];
$filesize = $_FILES['image']['size'];
$filetype = $_FILES['image']['type'];

if ($filename) 
{
	$filename = stripslashes($_FILES['image']['name']);

	$extension = getExtension($filename);
	$extension = strtolower($extension);

if($extension=='jpg' || $extension=='jpeg' || $extension=='pjpeg')
{
	$uploadedfile = $_FILES['image']['tmp_name'];
	$src = imagecreatefromjpeg($tmp_file);
}

elseif($extension=='png')
{
	$uploadedfile = $_FILES['image']['tmp_name'];
	$src = imagecreatefrompng($tmp_file);
}

else 
{
	$src = imagecreatefromgif($tmp_file);
}


$percent = 0.5;

list($width,$height)=getimagesize($tmp_file);

$newwidth=$width;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);

$newwidth1=$width*$percent;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);

$filePath = 'gallery/gallery/';
$filePathSmall = 'gallery/gallery-small/';

$filename = $filename.'.jpg';

$filenamesmall = $newheight1.'x'.$newheight1.'_'.$filename.'.jpg';

imagejpeg($tmp,$filePath.$filename,100);

imagejpeg($tmp1,$filePathSmall.$filenamesmall,100);

imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);

}

}

$query = “INSERT INTO gallery (name, name_small, size, type, path, path_small)
VALUES (’$filename’, ‘$filenamesmall’, ‘$filesize’, ‘$filetype’, ‘$filePath’, ‘$filePathSmall’)”;

$result = mysql_query($query) or die('Error, query failed : ’ . mysql_error());

if($result){
[/php]

upload form…

[php]

                <input type="file" name="image" id="file"/>
                
                <input type="submit" name="photo_submit" value="Upload"/>
                
            </form>[/php]

Instead of giving us just your code, it would be much better to post the errors you get.

I don’t work with GD that much but,
[php]imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);[/php]

Doesn’t that destroy the image? If the image is destoryed it won’t be uploaded also you don’t need this:

[php]<?php $_SERVER['PHP_SELF']; ?>[/php]

change it to:
[php][/php]
Or leave it empty.

Yes, the error that I am getting is no data is uploaded to my database, if you’re not helping don’t respond…

the images that are being destroyed are the “temporary” photos. The photos should already be uploaded to the folder at that point

please note this section
[php]imagejpeg($tmp,$filePath.$filename,100);[/php]

You need to do error checking in each condition to find where the problem is happening, so use

var_dump() to find where the problem is occurring, once you do that it should be easier to solve.

Also:
[php]$filePath = ‘gallery/gallery/’;
$filePathSmall = ‘gallery/gallery-small/’;[/php]

Are you sure this doesn’t need to be the full path?

I’m not getting any error messages.

It appears as I am not uploading any photos. I believe there is something little that I am missing but I have not been able to find it.

[php]if($_SERVER[‘REQUEST_METHOD’] == ‘POST’) {

function getExtension($str) {
$i = strrpos($str,’.’);
if (!$i) { return ‘’; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;

$filename = $_FILES['image']['name'];
$tmp_file = $_FILES['image']['tmp_name'];
$filesize = $_FILES['image']['size'];
$filetype = $_FILES['image']['type'];

var_dump($filename);
var_dump($tmp_file);

if ($filename) 
{

            var_dump($filename);
	$filename = stripslashes($_FILES['image']['name']);

	$extension = getExtension($filename);
	$extension = strtolower($extension);

if($extension=='jpg' || $extension=='jpeg' || $extension=='pjpeg')
{
	$uploadedfile = $_FILES['image']['tmp_name'];
            var_dump($uploadedfile);
	$src = imagecreatefromjpeg($tmp_file);
}

elseif($extension=='png')
{
	$uploadedfile = $_FILES['image']['tmp_name'];
	$src = imagecreatefrompng($tmp_file);
}

else 
{
	$src = imagecreatefromgif($tmp_file);
}


$percent = 0.5;

list($width,$height)=getimagesize($tmp_file);

$newwidth=$width;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);

$newwidth1=$width*$percent;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);

$filePath = 'gallery/gallery/';
$filePathSmall = 'gallery/gallery-small/';

$filename = $filename.'.jpg';

$filenamesmall = $newheight1.'x'.$newheight1.'_'.$filename.'.jpg';

imagejpeg($tmp,$filePath.$filename,100);

imagejpeg($tmp1,$filePathSmall.$filenamesmall,100);

imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);

}

}

$query = “INSERT INTO gallery (name, name_small, size, type, path, path_small)
VALUES (’$filename’, ‘$filenamesmall’, ‘$filesize’, ‘$filetype’, ‘$filePath’, ‘$filePathSmall’)”;

$result = mysql_query($query) or die('Error, query failed : ’ . mysql_error());
[/php]

I didn’t mean checking for a literal error, I mean find where the vars are returning empty.

I didn’t add all the var dumps you can make, just added a few for an example. Add more to check all conditions.

Sponsor our Newsletter | Privacy Policy | Terms of Service