Jump to content

How to handle uploaded picture (move_uploaded_file)


Johan Beijar

Recommended Posts

Dear all,

 

I have a challenge with a function that should add an uploaded picture. what the function should do is to check the sixe of the picture, if too large then resize it, move it to correct folder and update the DB.

 

It is these lines that makes the error msg that I get:

 

	if (!move_uploaded_file($tmpname, $final_fqfn)) {
$result['errors'] .= sprintf(plog_tr('Could not move uploaded file: %s to %s'), '<strong>'.$tmpname.'</strong>', '<strong>'.$final_fqfn.'</strong>');

 

I the the error msg from this IF-statement after I added these lines;

 

 

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

/*
End resize of actual picture
*/
$tmpname=$image;

 

Why will not PHP move the temp-file for me and what should I do about it?

Any input is good...

 

Thank you very much!

 

/Johan

 

Full function:

 

function add_picture($album_id, $tmpname, $filename, $caption, $desc, $lat, $lng, $statcode, $statreq, $address,$acc, $country, $allow_comm = 1) {

global $config;


/*
settings mainly for the resize of pic
*/
$maxsize=1048576;                //in bytes (default 1048576)

$perserve_ratio=true;        //if true prevents distortion

$max_width=420;                        //in pixels, must be greater than 0

$max_height=350;                //in pixels, must be greater than 0

$output_type=0;                        //determines type:
                                                //0 - jpg
                                                //1 - png
                                                //2 - gif
                                                //3 - same as uploaded file

$jpg_quality=100;                //if jpg selected, how good should the quality be (0 - 100)

$output_setting=0;                //0, outputs image to browser
                                                //1, outputs image to a file
                                                //2, outputs to file and browser


/*
end settings
*/

/*
Resize of actual picture
*/
//convert size while saving ratio
            $size = getimagesize($tmpname);
           $width=$size[0];
           $height=$size[1];

                if (($width > $max_width) || ($height > $max_height))
                {
                        if ($perserve_ratio==true)
                        {
                                if ($width > $height)
                                {
                                        $new_width=$max_width;
                                        $new_height=(($max_height * $height) / $width);
                                }
                                else if ($height < $width)
                                {
                                        $new_height=$max_height;
                                        $new_width=(($max_width * $width) / $height);
                                }
                                else
                                {
                                        $new_width=$max_width;
                                        $new_height=$max_height;
                                }
                        }
                        else
                        {
                                $new_width=$max_width;
                                $new_height=$max_height;
                        }

                }
                else
                {
                        $new_width=$width;
                        $new_height=$height;
                }
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

/*
End resize of actual picture
*/
$tmpname=$image;

$filename_parts = explode('.', strrev($filename), 2);
$filename_base = strrev($filename_parts[1]);
$filename_ext = strrev($filename_parts[0]);

$result = array(
	'output' => '',
	'errors' => '',
	'picture_id' => false,
);

$i = 0;

$unique_filename_base = strtolower(sanitize_filename(SmartStripSlashes($filename_base)));

// Now get the name of the collection
$sql = "SELECT c.path AS collection_path, c.id AS collection_id,
		a.path AS album_path, a.id AS album_id
		FROM ".PLOGGER_TABLE_PREFIX."albums a, ".PLOGGER_TABLE_PREFIX."collections c
		WHERE c.id = a.parent_id AND a.id = '$album_id' and a.username='$username'";

$sql_result = run_query($sql);
$albumdata = mysql_fetch_assoc($sql_result);

// This shouldn't happen in normal cases
if (empty($albumdata)) {
	$result['errors'] .= plog_tr('No such album!');
	return $result;
}

$dest_album_name = SmartStripSlashes($albumdata['album_path']);
$dest_collection_name = SmartStripSlashes($albumdata['collection_path']);
//Added username to the path. 
$create_path = $username.'/'.$dest_collection_name.'/'.$dest_album_name;

while (is_file($config['basedir'].'plog-content/images/'.$create_path.'/'.$unique_filename_base.'.'.$filename_ext)) {
	$unique_filename_base = SmartStripSlashes($filename_base).'('.++$i.')';
}

$final_filename = sanitize_filename($unique_filename_base).'.'.$filename_ext;

// Final fully qualified filename
$final_fqfn = $config['basedir'].'plog-content/images/'.$create_path.'/'.$final_filename;

if (!makeDirs($config['basedir'].'plog-content/images/'.$create_path)) {
	$result['errors'] .= sprintf(plog_tr('Could not create directory %s!'), '<strong>'.$create_path.'</strong>');
	return $result;
}


if (is_uploaded_file($tmpname)) {
	// If safe_mode enabled, open the permissions if the destination path
	if (is_safe_mode()) {
		$parent_path = $config['basedir'].'plog-content/images/'.$create_path;
		chmod_ftp($parent_path, 0777);
	}
	if (!move_uploaded_file($tmpname, $final_fqfn)) {
		$result['errors'] .= sprintf(plog_tr('Could not move uploaded file: %s to %s'), '<strong>'.$tmpname.'</strong>', '<strong>'.$final_fqfn.'</strong>');
	}
	// If safe_mode enabled, close the permissions back down to the default
	if (is_safe_mode()) {
		chmod_ftp($parent_path);
	}
} else {
	if (!move_this($tmpname, $final_fqfn)) {
		$result['errors'] .= sprintf(plog_tr('Could not move file: %s to %s'), '<strong>'.$tmpname.'</strong>', '<strong>'.$final_fqfn.'</strong>');
	}
}
//If any problems with the file. End and return errormsg
if (empty($result['errors'])) {
	if (is_file($tmpname)) {
		kill_file($tmpname);
	}
	$res = @chmod($final_fqfn, PLOGGER_CHMOD_FILE);

	// Get the EXIF data.
	require_once(PLOGGER_DIR.'plog-includes/lib/exifer1_7/exif.php');
	$exif_raw = read_exif_data_raw($final_fqfn, false);
	$exif = array();

	$exif['date_taken'] = (isset($exif_raw['SubIFD']['DateTimeOriginal'])) ? trim($exif_raw['SubIFD']['DateTimeOriginal']) : '';
	$exif['camera'] = (isset($exif_raw['IFD0']['Make']) && isset($exif_raw['IFD0']['Model'])) ? trim($exif_raw['IFD0']['Make']).' '.trim($exif_raw['IFD0']['Model']) : '';
	$exif['shutter_speed'] = (isset($exif_raw['SubIFD']['ExposureTime'])) ? $exif_raw['SubIFD']['ExposureTime'] : '';
	$exif['focal_length'] = (isset($exif_raw['SubIFD']['FocalLength'])) ? $exif_raw['SubIFD']['FocalLength'] : '';
	$exif['flash'] = (isset($exif_raw['SubIFD']['Flash'])) ? $exif_raw['SubIFD']['Flash'] : '';
	$exif['aperture'] = (isset($exif_raw['SubIFD']['FNumber'])) ? $exif_raw['SubIFD']['FNumber'] : '';
	$exif['iso'] = (isset($exif_raw['SubIFD']['ISOSpeedRatings'])) ? $exif_raw['SubIFD']['ISOSpeedRatings'] : '';

	$picture_path = $create_path.'/'.$final_filename;
	$username=$GLOBALS['username'];

	$query = "INSERT INTO `".PLOGGER_TABLE_PREFIX."pictures`
		(`parent_collection`,
		`parent_album`,
		`path`,
		`date_modified`,
		`date_submitted`,
		`allow_comments`,
		`EXIF_date_taken`,
		`EXIF_camera`,
		`EXIF_shutterspeed`,
		`EXIF_focallength`,
		`EXIF_flash`,
		`EXIF_aperture`,
		`EXIF_iso`,
		`caption`,
		`description`,
		`username`,
		`lat`,
		`lng`,
		`statcode`,
		`statreq`,
		`address`,
		`acc`,
		`country` )
		VALUES
			('".$albumdata['collection_id']."',
			'".$albumdata['album_id']."',
			'".mysql_real_escape_string($picture_path)."',
			NOW(),
			NOW(),
			".intval($allow_comm).",
			'".mysql_real_escape_string($exif['date_taken'])."',
			'".mysql_real_escape_string($exif['camera'])."',
			'".mysql_real_escape_string($exif['shutter_speed'])."',
			'".mysql_real_escape_string($exif['focal_length'])."',
			'".mysql_real_escape_string($exif['flash'])."',
			'".mysql_real_escape_string($exif['aperture'])."',
			'".mysql_real_escape_string($exif['iso'])."',
			'".mysql_real_escape_string($caption)."',
			'".mysql_real_escape_string($desc)."',
			'$username',
			'".$lat."',
			'".$lng."',
			'".$statcode."',
			'$statreq',
			'$address',
			'".$acc."',
			'$country')";

	$sql_result = run_query($query);

	$result['output'] .= sprintf(plog_tr('Your image %s was uploaded successfully.'), '<strong>'.$filename.'</strong>');
	$result['picture_id'] = mysql_insert_id();


			$thumbpath = generate_thumb($picture_path, $result['picture_id'], THUMB_SMALL);

		$queryupdpic = "UPDATE ".PLOGGER_TABLE_PREFIX."pictures SET
		thumbpath='$thumbpath'
		WHERE id= ".$result['picture_id']."";
		$resultupdpic = mysql_query($queryupdpic);
}

return $result;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.