Jump to content
Larry Ullman's Book Forums

Chapter 10: Web Application Development Script 10.3 - Upload_Image.Php


Recommended Posts

I was able to code this script without any problems, I understand the process of uploading a file from apache's default temporary directory to the uploads folder.

 

My question is:

 

Is this tied to inserting an image into a database? Or an alternative method? If I wanted to tie certain images to a certain users unique id, it seems like uploading a file into a directory wouldn't work.

 

If inserting an image into a database is the solution to my question, would the correct datatype be LONGBLOB? Again, I'm just guessing after some research this seems to be the solution.

 

Thanks

Mark

Link to comment
Share on other sites

You can easily upload a file to a directory and assign the image any name you want, weather a user id or random name.

I read the book a long time ago and I don't remember exactly the exaple but I think there was a problem with the uploading script... I guess the image is uploaded without the extenstion and thats why you why try to access it , it doesn't work.

 

I will provide you with a code I use to upload image to a directory.

In my case use random name for my image and I save it in the database with the associated record.

You don't have to name the image with the user ID and rather than that you can save its name in the database and refer to it by a fk

Link to comment
Share on other sites

Moving it from the temp directory to a permanent directory is an alternative to storing the image in a database. You can definitely associate images with a user ID using the file system. Storing in the database would be as a LONGBLOG. And you'll need a dedicated PHP script to retrieve an image, send the proper headers, and then send the file to the browser.

Link to comment
Share on other sites

here is the function to validat the file

 

function ValidateUpload($File)

{

global $mysqli;

if(!empty($File))

{

$errors = array();

// get the file from user

$fileName = basename($File['name']);

$tempFilename = $File['tmp_name'];

$fileSize = $File['size'];

$fileType = strtolower($File['type']);

$fileExtension = strtolower(substr(strchr($fileName,'.'), 1));

$imageInfo = getimagesize($tempFilename);

$imageInfo = $imageInfo['mime'];

$width= $imageInfo[0];

$height = $imageInfo[1];

//Convert all applicable characters to HTML entities

$fileName = htmlentities($fileName);

$fileType = htmlentities($fileType);

//Validate the type.Should be JPEG , PNG Or GIF

$allowed = array('image/pjepg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/PNG',

'image/png', 'image/x-png', 'image/gif', 'image/GIF');

if(!in_array($fileType, $allowed))

{

$errors[]="<p>إمتداد الملف الذي قمت بتحميله غير مصرح به.</p>";

}// end of if(in_array($_FILES['upload']['type'], $allowed)){

if(!in_array($imageInfo, $allowed))

{

$errors[]="<p>- نوع الملف الذي تحاوله رفعه غير معروف.</p>";

}

if(!is_numeric($fileSize))

{

$errors[]="<p>- الملف الذي تحاول رفعه غير مصرح به.</p>";

}

}//End if(!empty($_FILES['upload']['name']))

return $errors;

}

 

 

 

 

and this is the function to upload the file

 

function UploadFile($File,$tempFilename, $fileName,$fileExtension)

{

global $mysqli;

 

if(!empty($File))

{

$targetPath = "../uploads/";

if(move_uploaded_file($tempFilename, $targetPath.$fileName))

{

if(file_exists($targetPath.$fileName))

{

//Generate uniqe file name

$Image = uniqid(md5(time())).".".$fileExtension;

//Rename the file to an uniqid version

rename($targetPath . $fileName, $targetPath . $Image);

}//end of if(file_exists($target_path/$filename)){

}//end of if(move_uploaded_file($temp_filename, $target_path.$file_name)){

} //END if(!empty($_FILES['Image']['name'])){

return $Image;

}

 

 

and here is an example of how I use them in my code

 

if(!empty($_FILES['Image']['name']))

{

$File = $_FILES['Image'];

$errors = ValidateUpload($File);

}//End if(!empty($_FILES['upload']['name']))

else

{

$Image = NULL;

}

 

 

 

 

if(!empty($_FILES['Image']['name']))

{

$File = $_FILES['Image']['name'];

$fileName = basename($_FILES['Image']['name']);

$tempFilename = $_FILES['Image']['tmp_name'];

$fileExtension = strtolower(substr(strchr($fileName,'.'), 1));

$Image = UploadFile($File,$tempFilename, $fileName,$fileExtension);

}//END if(!empty($_FILES['Image']['name']))

Link to comment
Share on other sites

Thanks Larry i was just curious if it would be sufficient using one method, or if using both methods is more preferred? I am looking for the most practical and popular solution.

 

Bahaa - thanks for the script, Larry's book provides a similar example. Yours doesn't include an INSERT query though. Are you also inserting the image into the database, at the same time inserting it in the uploads directory? And if so, then why did you have to use both methods.

Link to comment
Share on other sites

I am only inserting the name of the image to the database not the image its self and I upload the image to a folder on the server.

I did not show the insert query because it works the same as other insert quiers.

You insert the name of the image same any other data you insert to the database.

 

If you need more clarification let me know.

Link to comment
Share on other sites

got it. you are using a combination of the two. are you getting errors with the file name extensions being included when inserting into the database?

 

When you call your images, do you call them from the database or the directory?

 

I don't get an errors when I insert the image name into the database.

when you call your image you need to put the path to your image directory and add the variable name that represent the image name in the database.

 

something like this <img src="../upload/<?php echo $image; ?>" width="" height="" alt ="">

Link to comment
Share on other sites

Thanks bahaa, exactly what I was looking for.

 

Larry mentions above that there are proper headers while retrieving an image, which one did you use?

 

 

You use the header to retrive the image if you put the image directory out of the root, but if you don't but it out of the root you don't need to use header to retrive the image, you just do like I show you above.

Link to comment
Share on other sites

 Share

×
×
  • Create New...