0

I wanted to how do we upload an image path(url) into the MySql database and call it through PHP. Do we use the BLOB function, if yes, then how? I don't want to upload the image itself as it will burden the database.

Please Advise.*

* Any Tutorials, videos, article, books......pls include the links

Thanks.

4
Contributors
11
Replies
12
Views
8 Years
Discussion Span
Last Post by lordx78
0

Hi...something like this may help get you pointed in the right direction

<?
php require_once('/Connections/connect.php'); 

$colname_rsProject = "-1";
if (isset($_GET['ID'])) {
  $colname_rsProjects = (get_magic_quotes_gpc()) ? $_GET['ID'] : addslashes($_GET['ID']);
}
mysql_select_db($database, $conn);
$query_rsProject = sprintf("SELECT * FROM dbtable WHERE ID = %s", $colname_rsProject);
$rsProject = mysql_query($query_rsProject, $conn) or die(mysql_error());
$row_rsProject = mysql_fetch_assoc($rsProject);
$totalRows_rsProject = mysql_num_rows($rsProject);


define ('MAX_FILE_SIZE', 2048000);  
$AddData="No";
$MM_flag="MM_update";
if((isset($_POST["submit"])) && ($_POST["MM_update"] == "formname")) {
	$AddData="Yes";
	$startwarn="No";
 /// image/file upload code begin	
	
		if (array_key_exists('submit', $_POST)) {
  			// define constant for upload folder
  			define('UPLOAD_DIR', 'you upload directory info here');
  			// replace any spaces in original filename with underscores
  			// and assign to a simpler variable
  			$file = str_replace(' ', '_', $_FILES['image']['name']);
  			// convert the maximum size to KB
  			$max = number_format(MAX_FILE_SIZE/1024, 1).'KB';
  			// create an array of permitted MIME types
  			$permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
  			// begin by assuming the file is unacceptable
  			$size_OK = false;
  			$type_OK = false;
  
  			// check that file is within the permitted size
  			if ($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
    			$size_OK = true;
			}

  			// check that file is of an permitted MIME type
  			foreach ($permitted as $type) {
    		if ($type == $_FILES['image']['type']) {
     		 $type_OK = true;
	  		break;
	 		 }
			}
  
  
  		if ($size_OK && $type_OK) {
    		switch($_FILES['image']['error']) {
	  		case 0:
        	// move the file to the upload folder and rename it
			$success_OK = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$file);
			if ($success_OK) {
          		$result1 = "$file  has been uploaded successfully";
	     	 }
			else {
			$AddData = "No";
		  	$result = "There was an error uploading $file. Please try again.";
		  	}
	    	break;
	  		case 3:
			$AddData = "No";
			$result = "There was an error uploading $file. Please try again.";
	  		default:
			$AddData = "No";
        	$result = "System error uploading $file. Please contact the webmaster.";
	  		}
   		 }
  		elseif ($_FILES['image']['error'] == 4) {
			
    		$result1 = 'No file selected';
			}
  		else {
			$AddData = "No";
    		$result = "$file cannot be uploaded. Maximum size: $max. Acceptable file types:  gif, jpg, png.";
			}
 		 }
		 
	if (isset($result)) {
			$AddData = "No";
		if ($startwarn=="No") { ?>
		<div class="startwarning">Please complete the missing item(s) indicated. </div>
			<?php $startwarn="Yes"; }  ?>
			<div class="warning"><li><?php echo $result; ?></li></div>

<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}


$ID = $_POST['ID'];
mysql_select_db($database, $conn);
$query_rsUploads = ("SELECT * FROM db WHERE ID = '$ID' and FilePath = '$file'");
$rsUpload = mysql_query($query_rsUpload, $conn) or die(mysql_error());
$row_rsUpload = mysql_fetch_assoc($rsUpload);
$totalRows_rsUpload = mysql_num_rows($rsUpload);

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "formname") && $AddData!="No" && $totalRows_rsUpload == 0 && $file !="") {
  $insert = sprintf("INSERT INTO dbtable (ID, FilePath) VALUES (%s, '$file')",
                       
                       GetSQLValueString($_POST['ID'], "int"));

mysql_select_db($database, $conn);
$query_rsUploadFile = sprintf("SELECT * FROM dbtable WHERE ID = %s", $colname_rsProject);
$rsUploadFile = mysql_query($query_rsUploadFile, $conn) or die(mysql_error());
$row_rsUploadFile = mysql_fetch_assoc($rsUploadFile);
$totalRows_rsUploadFile = mysql_num_rows($rsUploadFile);		 
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
  .warning {
	background-color: #ffffee;
	color: #660000;
	width: 650px;
	padding: 5px;
	border-right-width: thin;
	border-left-width: thin;
	border-right-style: solid;
	border-left-style: solid;
	border-right-color: #FF0000;
	border-left-color: #FF0000;
	text-indent: 20px;
  }
   .warningend {
	background-color: #ffffee;
	color: #660000;
	width: 650px;
	padding: 5px;
	border-right-width: thin;
	border-left-width: thin;
	border-right-style: solid;
	border-left-style: solid;
	border-right-color: #FF0000;
	border-left-color: #FF0000;
	border-bottom-width: thin;
	border-bottom-style: solid;
	border-bottom-color: #FF0000;
  }
    .startwarning {
	background-color: #ffffee;
	color: #660000;
	width: 650px;
	padding: 5px;
	border-right-width: thin;
	border-left-width: thin;
	border-right-style: solid;
	border-left-style: solid;
	border-right-color: #FF0000;
	border-left-color: #FF0000;
	border-top-width: thin;
	border-top-style: solid;
	border-top-color: #FF0000;
  }
  .notify {
    border: 1px solid #336699;
    background-color: #ffffee;
    color: #336699;
    width: 650px;
    padding: 5px;
  }
-->
</style>

</head>

<body>
<form method="post" name="formname" action="<?php echo $editFormAction; ?>" enctype="multipart/form-data"/>
<?php
if (array_key_exists('submit', $_POST)) {
}
if($AddData=="no") {
?>
<input type="hidden" name="ID" value="<?php echo $row_rsProject['ID']; ?>"  />
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
<input type="hidden" name="ID" value="<?php echo $row_rsProject['ID']; ?>">
<input type="file" name="image" id="image" size="70"  />
<input type="submit" name="submit" value="Submit" />
<input type="hidden" name="MM_update" value="formname">
</form>
<?php
 }
?>		
</body>
</html>
0

If you know a little bit PHP and MySQL there is no problem to accomplish your task. in your database should store only the file name (let say column name is image_name). you must define a constant with your upload folder something like: define("UPLOAD_DIR", "images_uploaded"); . when you will display the images you will refer as follows:

// do whatever (db) processing is necessarily

while ($obj = mysql_fetch_object($rez))
{
echo "<img src='./".UPLOAD_DIR."/".$obj->image_name."' alt='some alt desc'>";
}


hope it's useful ...

0

I wanted to how do we upload an image path(url) into the MySql database and call it through PHP.

When you upload an image, you upload it to a path. Store the path in a variable and at the time you upload, insert a record to the table containing the path.

Do we use the BLOB function, if yes, then how?

Nope. If you are uploading only the path, a fieldname of datatype varchar will do. eg. varchar(100).

I don't want to upload the image itself as it will burden the database.

Exactly ! Its not a good idea to upload images to the database.

0

When you upload an image, you upload it to a path. Store the path in a variable and at the time you upload, insert a record to the table containing the path.

Can you show me an example of doing this process. thanks.

0

I've visited the given url. But one question....

That's the path. Save that in the table.

How do I save it in the table. How does he path look like. Pls advise, still a bit blur on this part.

0

I don't know if you read (or understand) my previous post....
in the database you should keep only the file name. let say your file is image1.jpg and it is stored in {root}/images/uploaded_images/ + image1.jpg. why i think it's a better idea to keep only the filename? think what will happen if you store something like: "images/uploaded_images/image1.jpg" and one day you decide to rename your folder from "uploaded_images" to "uploads" ....

0

yes, I've read it but just a bit confusing. Anyhow thanks for your guide.

0
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

Can't we customize this input box, I mean the length of the text area, rename the browse button. Can we?
0

you cannot use something like class='....' or style='...' to customize the file control .... it can be done but you have to dig a lot ....

This article has been dead for over six months. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.