Jump to content

Send uploaded file to directory on web server


adam291086

Recommended Posts

hello,

 

My up load form sort of work. The information on the file location is added into the database but the file itself isn't loaded into the directory. Can anyone see why

 

<?php


   // Define script constants
      DEFINE("IMAGE_BASE", '../photos/');
      DEFINE("THUMB_BASE", '../thumbs/');
      DEFINE("MAX_WIDTH", 100);
      DEFINE("MAX_HEIGHT", 100);
      DEFINE("RESIZE_WIDTH", 800);
      DEFINE("RESIZE_HEIGHT", 600);


class GallerySizer{
      var $img;     // Original image file object
      var $thumb;          // Thumbnail file object
      var $resize;         // Resized image file name
      var $width;          // Original image width
      var $height;         // Original image height
      var $new_width;      // Resized image width
      var $new_height;     // Resized image height
      var $image_path;     // Path to image
      var $thumbscale;     // Scale to resize thumbnail
      var $image_file;     // Resized image filename
      var $thumbnail;      // Thumbnail image file object
      var $random_file;    // Resized image file name (random)

/*****
     * Retrieves path to uploaded image.
     * Retrieves filename of uploaded image
     */
    function getLocation($image){
        $this->image_file = str_replace("..", "/", $image);
        $this->image_path = IMAGE_BASE . $this->image_file;
        return true;
    }

/*****
    * Determines image type, and creates an image object
    */
    function loadImage(){
        $this->img = null;
        $extension = strtolower(end(explode('.', $this->image_path)));
        if ($extension == 'jpg' || $extension == 'jpeg'){
            $this->img = imagecreatefromjpeg($this->image_path);
        } else if ($extension == 'png'){
            $this->img = imagecreatefrompng($this->image_path);
        } else {
            return false;
        }
// Sets a random name for the image based on the extension type
       $file_name = strtolower(current(explode('.', $this->image_file)));
        $this->random_file = $file_name . $this->getRandom() . "." . $extension;
        $this->thumbnail = $this->random_file;
        $this->converted = $this->random_file;
        $this->resize = $this->random_file;
        return true;
    }

/*****
* Retrieves size of original image.  Sets the conversion scale for both         *   the thumbnail and resized image
*/
function getSize(){
      if ($this->img){
$this->width = imagesx($this->img);
$this->height = imagesy($this->img);
$this->thumbscale = min(MAX_WIDTH / $this->width, MAX_HEIGHT / $this->height);
      } else {
          return false;
      }
       return true;
}


/*****
     * Creates a thumbnail image from the original uploaded image
     */
    function setThumbnail(){
        // Check if image is larger than max size
        if ($this->thumbscale < 1){
            $this->new_width = floor($this->thumbscale * $this->width);
            $this->new_height = floor($this->thumbscale * $this->height);
            // Create temp image
            $tmp_img = imagecreatetruecolor($this->new_width, $this->new_height);
            // Copy and resize old image into new
            imagecopyresampled($tmp_img, $this->img, 0, 0, 0, 0, $this->new_width, $this->new_height, $this->width, $this->height);
            $this->thumb = $tmp_img;
        }
        return true;
    }

  /*****
     * Resizes uploaded image to desired viewing size
     */
    function resizeImage(){
     if ($this->width < RESIZE_WIDTH){
         $this->resize = $this->img;
            return true;
        } else {
            // Create re-sized image
            $tmp_resize = imagecreatetruecolor(RESIZE_WIDTH, RESIZE_HEIGHT);
            // Copy and resize image
            imagecopyresized($tmp_resize, $this->img, 0, 0, 0, 0, RESIZE_WIDTH, RESIZE_HEIGHT, $this->width, $this->height);
            imagedestroy($this->img);
            $this->resize = $tmp_resize;
            return true;
        }
    }

/*****
     * Copies thumbnail image to specified thumbnail directory.
     * Sets permissions on file
     */
    function copyThumbImage(){
imagejpeg($this->thumb, $this->thumbnail);
        if(!@copy($this->thumbnail, THUMB_BASE . $this->thumbnail)){
            echo("Error processing file... Please try again!");
            return false;
        }
        if(!@chmod($this->thumbnail, 666)){
            echo("Error processing file... Please try again!");
            return false;
        }
        if(!@unlink($this->thumbnail)){
            echo("Error processing file... Please try again!");
            return false;
        }
        return true;
    }

/*****
     * Copies the resized image to the specified images directory.
     * Sets permissions on file.
     */
    function copyResizedImage(){
        imagejpeg($this->resize, $this->converted);
        if(!@copy($this->converted, IMAGE_BASE . $this->converted)){
            echo("Error processing file... Please try again!");
            return false;
        }
        if(!@chmod($this->converted, 666)){
            echo("Error processing file... Please try again!");
            return false;
        }
        if(!unlink($this->converted)){
            echo("Error processing file... Please try again!");
            return false;
        }
        // Delete the original uploaded image
        if(!unlink(IMAGE_BASE . $this->image_file)){
            echo("Error processing file... Please try again!");
            return false;
        }
        return true;
    }

  /*****
     * Generates a random number.  Random number is used to rename
     * the original uploaded image, once resized.
     */
    function getRandom(){        
        return "_" . date("dmy_His");
    }


   /*****
     * Returns path to thumbnail image
     */
    function getThumbLocation(){
        return "../thumbs/" . $this->random_file;
    }
/*****
     * Returns path to resized image
     */
    function getImageLocation(){
        return "../photos/" . $this->random_file;
    }

}

?>

Link to comment
Share on other sites

he is the code that calls upon the gallerysizer

 

 
<?php
include("GallerySizer.php");
include("config.php");
DEFINE("IMAGE_FULL", '../photos/');



//database connect


$db_server ="db1125.oneandone.co.uk";
     $db_user = "dbo218351273";
     $db_pass = "adawman9772";
     $db_name = "db218351273";

$con = mysql_connect("$db_server","$db_user","$db_pass");


// Album cover
$cover = $_FILES['image']['name'][$_POST['album_cover']];
if (!$_REQUEST['process']){
echo("Error! No images chosen for processing. <br />");
echo("Click <a href='index.php'>here</a> to start processing your images.");
die();
} elseif (!$_POST['album_id']){
echo("No album selected. Please <a href=\"\">choose an album</a> to upload images to.");
die();
} else {
for($i = 0; $i < count($_FILES['image']['tmp_name']); $i++){
$fileName = $_FILES['image']['name'][$i];
copy($_FILES['image']['tmp_name'][$i], IMAGE_FULL . $fileName);
$thumb = new GallerySizer();
if($thumb->getLocation($_FILES['image']['name'][$i])){
if($thumb->loadImage()){
echo("Still here!");
if($thumb->getSize()){
if($thumb->setThumbnail()){
if($thumb->copyThumbImage()){
if($thumb->resizeImage()){
$thumb->copyResizedImage();

}
}
}
}
}
}
else {
echo("Invalid image/file has been uploaded. Please upload a supported file-type (JPEG/PNG)"); 
unlink(IMAGE_FULL . $fileName);
die();
}
insert_location($thumb);

}
} 

function insert_location($thumb_obj){

$image_location = $thumb_obj->getImageLocation();
$thumb_location = $thumb_obj->getThumbLocation();

// If image matches cover selection, update album cover
if($cover == $_FILES['image']['name'][$i]){
$sql = "UPDATE albums SET album_cover = '" . $thumb_location . "' WHERE album_id = " . $_POST['album_id'];
$result = @mysql_query($sql) or die("Error inserting records: " . mysql_error());


}

mysql_select_db("db218351273", $con);
$sql = ("INSERT INTO photos (photo_id, photo_title, photo_desc, photo_date, photo_location, thumbnail_location,album_id) 
VALUES('0', '$_POST[photo_title]', '$_POST[photo_desc]', 'NOW()', '$image_location', '$thumb_location', '$_POST[album_id]')");
$result = mysql_query($sql) or die("Error inserting record(s) into the database: " . mysql_error());
if ($result){
echo("Images successfully converted and stored! <br />Click <a href='index.html'>here</a> to continue.");


}
}

?>

 

Everything is update but nothing is being inserted into the folders that store the pics and thumbnails

Link to comment
Share on other sites

a little debugging needed it think

 

replace

$thumb = new GallerySizer();
if($thumb->getLocation($_FILES['image']['name'][$i])){
if($thumb->loadImage()){
echo("Still here!");
if($thumb->getSize()){
if($thumb->setThumbnail()){
if($thumb->copyThumbImage()){
if($thumb->resizeImage()){
$thumb->copyResizedImage();

}
}
}
}
}
}

 

with

$thumb = new GallerySizer();
if($thumb->getLocation($_FILES['image']['name'][$i]))
{
if($thumb->loadImage())
{
	echo("Still here!");
	if($thumb->getSize())
	{
		if($thumb->setThumbnail())
		{
			if($thumb->copyThumbImage())
			{
				if($thumb->resizeImage())
				{
					$thumb->copyResizedImage();
				}else{die("ERROR:resizeImage");}
			}else{die("ERROR:copyThumbImage");}
		}else{die("ERROR:setThumbnail");}
	}else{die("ERROR:getSize");}
}else{die("ERROR:loadImage");}
}else{die("ERROR:getLocation");}

Link to comment
Share on other sites

is this correct

<?php
include("GallerySizer.php");
include("config.php");
DEFINE("IMAGE_FULL", '../photos/');



//database connect


$db_server ="db1125.oneandone.co.uk";
     $db_user = "dbo218351273";
     $db_pass = "adawman9772";
     $db_name = "db218351273";

$con = mysql_connect("$db_server","$db_user","$db_pass");


// Album cover
$cover = $_FILES['image']['name'][$_POST['album_cover']];
if (!$_REQUEST['process']){
echo("Error! No images chosen for processing. <br />");
echo("Click <a href='index.php'>here</a> to start processing your images.");
die();
} elseif (!$_POST['album_id']){
echo("No album selected. Please <a href=\"\">choose an album</a> to upload images to.");
die();
} else {
for($i = 0; $i < count($_FILES['image']['tmp_name']); $i++){
$fileName = $_FILES['image']['name'][$i];
copy($_FILES['image']['tmp_name'][$i], IMAGE_FULL . $fileName);
$thumb = new GallerySizer();
if($thumb->getLocation($_FILES['image']['name'][$i])){
if($thumb->loadImage()){
echo("Still here!");
$thumb = new GallerySizer();
if($thumb->getLocation($_FILES['image']['name'][$i]))
{
if($thumb->loadImage())
{
	echo("Still here!");
	if($thumb->getSize())
	{
		if($thumb->setThumbnail())
		{
			if($thumb->copyThumbImage())
			{
				if($thumb->resizeImage())
				{
					$thumb->copyResizedImage();
				}else{die("ERROR:resizeImage");}
			}else{die("ERROR:copyThumbImage");}
		}else{die("ERROR:setThumbnail");}
	}else{die("ERROR:getSize");}
}else{die("ERROR:loadImage");}
}else{die("ERROR:getLocation");}
echo("Invalid image/file has been uploaded. Please upload a supported file-type (JPEG/PNG)"); 
unlink(IMAGE_FULL . $fileName);
die();
}
insert_location($thumb);

}
} 

function insert_location($thumb_obj){

$image_location = $thumb_obj->getImageLocation();
$thumb_location = $thumb_obj->getThumbLocation();

// If image matches cover selection, update album cover
if($cover == $_FILES['image']['name'][$i]){
$sql = "UPDATE albums SET album_cover = '" . $thumb_location . "' WHERE album_id = " . $_POST['album_id'];
$result = @mysql_query($sql) or die("Error inserting records: " . mysql_error());


}

mysql_select_db("db218351273", $con);
$sql = ("INSERT INTO photos (photo_id, photo_title, photo_desc, photo_date, photo_location, thumbnail_location,album_id) 
VALUES('0', '$_POST[photo_title]', '$_POST[photo_desc]', 'NOW()', '$image_location', '$thumb_location', '$_POST[album_id]')");
$result = mysql_query($sql) or die("Error inserting record(s) into the database: " . mysql_error());
if ($result){
echo("Images successfully converted and stored! <br />Click <a href='index.html'>here</a> to continue.");


}
}
}
?>

 

i now get the error Fatal error: Call to undefined function: insert_location() in /homepages/12/d214897219/htdocs/adam/create_thumbnails.php on line 61

Link to comment
Share on other sites

No, notice the duplicated code

$thumb = new GallerySizer(); #1
if($thumb->getLocation($_FILES['image']['name'][$i])){#1
if($thumb->loadImage()){
echo("Still here!");
$thumb = new GallerySizer();  #2
if($thumb->getLocation($_FILES['image']['name'][$i]))#2
{

 

try

<?php
include("GallerySizer.php");
include("config.php");
DEFINE("IMAGE_FULL", '../photos/');



//database connect


$db_server ="db1125.oneandone.co.uk";
     $db_user = "dbo218351273";
     $db_pass = "adawman9772";
     $db_name = "db218351273";

$con = mysql_connect("$db_server","$db_user","$db_pass");


// Album cover
$cover = $_FILES['image']['name'][$_POST['album_cover']];
if (!$_REQUEST['process']){
echo("Error! No images chosen for processing. <br />");
echo("Click <a href='index.php'>here</a> to start processing your images.");
die();
} elseif (!$_POST['album_id']){
echo("No album selected. Please <a href=\"\">choose an album</a> to upload images to.");
die();
} else {
for($i = 0; $i < count($_FILES['image']['tmp_name']); $i++){
$fileName = $_FILES['image']['name'][$i];
copy($_FILES['image']['tmp_name'][$i], IMAGE_FULL . $fileName);
$thumb = new GallerySizer();
//UPDATED
if($thumb->getLocation($_FILES['image']['name'][$i]))
{
if($thumb->loadImage())
{
	echo("Still here!");
	if($thumb->getSize())
	{
		if($thumb->setThumbnail())
		{
			if($thumb->copyThumbImage())
			{
				if($thumb->resizeImage())
				{
					$thumb->copyResizedImage();
				}else{die("ERROR:resizeImage");}
			}else{die("ERROR:copyThumbImage");}
		}else{die("ERROR:setThumbnail");}
	}else{die("ERROR:getSize");}
}else{die("ERROR:loadImage");}
}else{die("ERROR:getLocation");}
//END UPDATE
else {
echo("Invalid image/file has been uploaded. Please upload a supported file-type (JPEG/PNG)"); 
unlink(IMAGE_FULL . $fileName);
die();
}
insert_location($thumb);

}
} 

function insert_location($thumb_obj){

$image_location = $thumb_obj->getImageLocation();
$thumb_location = $thumb_obj->getThumbLocation();

// If image matches cover selection, update album cover
if($cover == $_FILES['image']['name'][$i]){
$sql = "UPDATE albums SET album_cover = '" . $thumb_location . "' WHERE album_id = " . $_POST['album_id'];
$result = @mysql_query($sql) or die("Error inserting records: " . mysql_error());


}

mysql_select_db("db218351273", $con);
$sql = ("INSERT INTO photos (photo_id, photo_title, photo_desc, photo_date, photo_location, thumbnail_location,album_id) 
VALUES('0', '$_POST[photo_title]', '$_POST[photo_desc]', 'NOW()', '$image_location', '$thumb_location', '$_POST[album_id]')");
$result = mysql_query($sql) or die("Error inserting record(s) into the database: " . mysql_error());
if ($result){
echo("Images successfully converted and stored! <br />Click <a href='index.html'>here</a> to continue.");


}
}

?>

Link to comment
Share on other sites

my bad,,

 

cleaned it up a little

<?php
include("GallerySizer.php");
include("config.php");
DEFINE("IMAGE_FULL", '../photos/');



//database connect


$db_server ="db1125.oneandone.co.uk";
     $db_user = "dbo218351273";
     $db_pass = "adawman9772";
     $db_name = "db218351273";

$con = mysql_connect("$db_server","$db_user","$db_pass");


// Album cover
$cover = $_FILES['image']['name'][$_POST['album_cover']];
if (!$_REQUEST['process']){
echo("Error! No images chosen for processing. <br />");
echo("Click <a href='index.php'>here</a> to start processing your images.");
die();
} elseif (!$_POST['album_id']){
echo("No album selected. Please <a href=\"\">choose an album</a> to upload images to.");
die();
} else {
for($i = 0; $i < count($_FILES['image']['tmp_name']); $i++){
	$fileName = $_FILES['image']['name'][$i];
	copy($_FILES['image']['tmp_name'][$i], IMAGE_FULL . $fileName);
	$thumb = new GallerySizer();
	if($thumb->getLocation($_FILES['image']['name'][$i])){
		if($thumb->loadImage()){
		echo("Still here!");
			if($thumb->getSize()){
				if($thumb->setThumbnail()){
					if($thumb->copyThumbImage()){
						if($thumb->resizeImage())
						{
							$thumb->copyResizedImage();
						}else{die("ERROR:resizeImage");}
					}else{die("ERROR:copyThumbImage");}
				}else{die("ERROR:setThumbnail");}
			}else{die("ERROR:getSize");}
		}else{die("ERROR:loadImage");}
	}else{
		echo("Invalid image/file has been uploaded. Please upload a supported file-type (JPEG/PNG)"); 
		unlink(IMAGE_FULL . $fileName);
		die();
	}
	insert_location($thumb);
}
} 

function insert_location($thumb_obj)
{

$image_location = $thumb_obj->getImageLocation();
$thumb_location = $thumb_obj->getThumbLocation();

// If image matches cover selection, update album cover
if($cover == $_FILES['image']['name'][$i]){
	$sql = "UPDATE albums SET album_cover = '" . $thumb_location . "' WHERE album_id = " . $_POST['album_id'];
	$result = @mysql_query($sql) or die("Error inserting records: " . mysql_error());
}

mysql_select_db("db218351273", $con);
$sql = ("INSERT INTO photos (photo_id, photo_title, photo_desc, photo_date, photo_location, thumbnail_location,album_id) 
VALUES('0', '$_POST[photo_title]', '$_POST[photo_desc]', 'NOW()', '$image_location', '$thumb_location', '$_POST[album_id]')");
$result = mysql_query($sql) or die("Error inserting record(s) into the database: " . mysql_error());
if ($result){
	echo("Images successfully converted and stored! <br />Click <a href='index.html'>here</a> to continue.");
}
}

?>

Link to comment
Share on other sites

OK thats means theirs a problem in the following code

<?php
    function loadImage(){
        $this->img = null;
        $extension = strtolower(end(explode('.', $this->image_path)));
        if ($extension == 'jpg' || $extension == 'jpeg'){
            $this->img = imagecreatefromjpeg($this->image_path);
        } else if ($extension == 'png'){
            $this->img = imagecreatefrompng($this->image_path);
        } else {
            return false; //<----This is being called
        }
// Sets a random name for the image based on the extension type
       $file_name = strtolower(current(explode('.', $this->image_file)));
        $this->random_file = $file_name . $this->getRandom() . "." . $extension;
        $this->thumbnail = $this->random_file;
        $this->converted = $this->random_file;
        $this->resize = $this->random_file;
        return true;
    }
?>

 

So the error is probably cause when the $this->image_name is being set, here

<?php
    function getLocation($image){
        $this->image_file = str_replace("..", "/", $image);
        $this->image_path = IMAGE_BASE . $this->image_file;
        return true;
    }
?>

 

which is being called from here

<?php
copy($_FILES['image']['tmp_name'][$i], IMAGE_FULL . $fileName);
$thumb = new GallerySizer();
if($thumb->getLocation($_FILES['image']['name'][$i])){ //<---HERE
?>

 

SO....

try changing

$thumb = new GallerySizer();
if($thumb->getLocation($_FILES['image']['name'][$i])){ 

to

$thumb = new GallerySizer();
if($thumb->getLocation(IMAGE_FULL . $fileName)){ 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.