Web Programming: How to upload an image, save its name on the database and saves the actual image on a directory using Php?
Syntax for uploading an image in Php script:
<?php
$allowedExts = array("jpg", "jpeg", "gif", "png"); //in this block of code, it will filter the allowed extensions to be uploaded
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 200000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
if (file_exists("upload/" . $_FILES["file"]["name"])) //upload is a folder that MUST exist inside the Php scripts directory
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
//locates the file
$ext = findexts ($_FILES['file']['name']) ;
//will set a random number for renaming image to allow duplication of uploads
$ran = rand () ;
//will include "Image" string in renaming the image
$ran2 = 'Image'.$ran.".";
//upload directory
$target_path = "upload/";
//appends the file
$target_path = $target_path . $ran2.$ext;
if(!move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
die("There was an error uploading the Photo, please try again!");
}
mysql_query("INSERT INTO photos (userid, photos)
VALUES ('$userid', '".$ran2.$ext."')") ;
echo "Image successfully uploaded!";
}
}
}
else
{
echo "Invalid file";
}
?>
Download my Web Programming Final Project by clicking here. It is open for development and modification.
<?php
$allowedExts = array("jpg", "jpeg", "gif", "png"); //in this block of code, it will filter the allowed extensions to be uploaded
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 200000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
if (file_exists("upload/" . $_FILES["file"]["name"])) //upload is a folder that MUST exist inside the Php scripts directory
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
//locates the file
$ext = findexts ($_FILES['file']['name']) ;
//will set a random number for renaming image to allow duplication of uploads
$ran = rand () ;
//will include "Image" string in renaming the image
$ran2 = 'Image'.$ran.".";
//upload directory
$target_path = "upload/";
//appends the file
$target_path = $target_path . $ran2.$ext;
if(!move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
die("There was an error uploading the Photo, please try again!");
}
mysql_query("INSERT INTO photos (userid, photos)
VALUES ('$userid', '".$ran2.$ext."')") ;
echo "Image successfully uploaded!";
}
}
}
else
{
echo "Invalid file";
}
?>
Download my Web Programming Final Project by clicking here. It is open for development and modification.