Jump to content

Recommended Posts

Hey,

 

I'm in the process of making a site that involves multiple layers of folders. I have a form that uploads an image which i then try to move to the image folder using a define function using the document root + image folder but the image never gets saved to the defined location and the location isnt inserted into the sql query. Its a simple thing I want to do but since Im making a bigger site I want to maximize includes to reduce duplicate code.

 

Include File - defines the document root

srvRoot = $_SERVER['DOCUMENT_ROOT'];
$serverRoot = str_replace('/', '\/', $serverRoot);
define('SRV_ROOT', $srvRoot);

 

Image Upload

$image = $_FILES["catImage"]["name"];
$image = addImage($image, SRV_ROOT . "images/shop/img_cat");

function addImage($image, $path){ // FIX FIX FIX
 $fileExtension = getExtension($image); //this function works fine have tested in previous scripts so not needed
 $image = substr(md5(rand() * time()), 0, 10).".".$fileExtension;
 $imgDestination = $path.$image;

 if($_FILES['catImage']['type'] != "image/gif" && $_FILES['catImage']['type'] != "image/jpg" && $_FILES['catImage']['type'] != "image/jpeg" && $_FILES['catImage']['type'] !="image/png" ){
	 echo "You may only upload image files";
 }else{
	 //Moves the uploaded file to destination folder via the variable which includes image filename 
	 if(move_uploaded_file($_FILES["catImage"]["tmp_name"],$imgDestination)){
		 return($imgDestination);
	 }else{
		 header("Location: ../../home.php");
	 }
	 echo $imgDestination;
}


}

Link to comment
https://forums.phpfreaks.com/topic/260102-problem-using-defined-document_root/
Share on other sites

Couple of questions:

 

What is the point of $serverRoot = str_replace('/', '\/', $serverRoot); and where is the variable $serverRoot used?

Where is the function addImage called? I only ask because I need to see how and where you define your second parameter, $path.

 

Oh ignore the $serverRoot str_replace it came with a snippet, just used to switch / with \, its doesnt get used in the script.

 

and

 

$path = SRV_ROOT . "images/shop/cat_img"

 

The idea is to set the image path created from $path + $imageName to a sql table where i can use it to display the image later on from any page on my site due to the path being absolute.

Here you go, thanks for having a look!

 

config.php

// Server root path
$srvRoot = $_SERVER['DOCUMENT_ROOT'];
//$serverRoot = str_replace('/', '\/', $serverRoot);

define('SRV_ROOT', $srvRoot);
//define('SRV_ROOT', $srvRoot);

// Image directory path
define('CATEGORY_IMAGE_PATH', 'lisahumphrey/images/shop/cat_img/');
define('CATEGORY_THUMB_PATH', 'images/shop/cat_thumb/');

 

database.php

$sqlCon = mysql_connect($dbHost, $dbUser, $dbPass) or die ('MYSQL connection Failed. ' . mysql_error());	

mysql_select_db($dbName) or die('Cannot select database. ' . mysql_error());

// Database query functions
function dbQuery($sql)
{
   return mysql_query($sql) or die('Query failed. ' . mysql_error());
}

 

addCategory.php

require_once('../include/database.php');
require_once('../../library/config.php');

function addCategory(){	
if(!isset($_POST["catName"], $_POST["catDescription"], $_FILES["catImage"])){
	header('Location: category_add.php');
}
$name = $_POST["catName"];
$description = $_POST["catDescription"];
$image = $_FILES["catImage"]["name"];
$image = addImage($image, SRV_ROOT);	 // FIX FIX FIX
$parentId = $_POST["parentCat"];
if(isset($_POST["isProduct"])){
	$isProduct = 1;
}else{
	$isProduct = 0;	
}

$sql = "INSERT INTO catagory_tbl (cat_parent_id, cat_isProduct, cat_name, cat_description, cat_thumbnail) VALUES ($parentId, $isProduct, '$name', '$description', '$image')";
$result = dbQuery($sql);
header('Location: category_list.php');
}

function addImage($image, $path){ // FIX FIX FIX
 $fileExtension = getExtension($image);
 $image = substr(md5(rand() * time()), 0, 10).".".$fileExtension;
 $imgDestination = $path.$image;

 if($_FILES['catImage']['type'] != "image/gif" && $_FILES['catImage']['type'] != "image/jpg" && $_FILES['catImage']['type'] != "image/jpeg" && $_FILES['catImage']['type'] !="image/png" ){
	 echo "You may only upload image files";
 }else{
	 //Moves the uploaded file to destination folder via the variable which includes image filename 
	 if(move_uploaded_file($_FILES["catImage"]["tmp_name"],$imgDestination)){ //MOVES IMAGE TO IMAGE DIR
		 return($imgDestination);
	 }else{
		 header("Location: ../../home.php");
	 }
	 echo $imgDestination;
}


}

function getExtension($fileName){
/* Gets image extension type from image name
 * Splits image name at the . to get name and extension.
 */
$fileNameParts = explode(".", $fileName);
// Retrieves image extension.
$fileExtension = end($fileNameParts);
// Converts extension string into all lowercase
$fileExtension = strtolower( $fileExtension );
// Returns fileExtension to previous code that called function getExtension
return($fileExtension);
}

So addCategory.php is what you call into the browser, or is there another file you call in the browser and addCategory.php is your action? What happens when you comment out the beginning and ending lines of each function and then try the upload?

this does talk to database.php

 

<body>
<?php require_once("../include/database.php")?>
<form id="addCategoryForm" method="POST" action="process_category.php" enctype="multipart/form-data">
<input type="text" id="catName" name="catName" value=""/>
<span>Will its display products?</span><input type="checkbox" id="isProduct" name="isProduct" />
<select name="parentCat">
<option name="noParent" value="0">Shop Homepage</option>
<?php
/*  Script below fills select options with avaliable parent categories
*  from catagory_tbl database using a simple loop.
*/
$result = mysql_query( "SELECT cat_id, cat_name
					FROM catagory_tbl 
					WHERE cat_isProduct=0 
					ORDER BY cat_id"
			 	  ) or die("Query Failed:" .mysql_error());;

//  Loop starts
if(dbNumRows($result) > 0){
while($row = dbFetchAssoc($result)){
	extract($row);
	echo "<option value='".$cat_id."'>".$cat_name."</option>";			
}
}
//  Loop stops
?>
</select>
<input type="text" id="catDescription" name="catDescription" value=""/>
<input type="file" id="catImage" name="catImage" />
<input type="submit" id="catSubmit" name="catSubmit" value="Submit"/>
<input type="submit" id="catCancel" name="catCancel" value="Cancel"/>
</form>

</body>

 

You have now got all my code lol

Let me know if I am reading this wrong. This to me says that when the form is not submitted, go back to category_add.php:

 

if(!isset($_POST["catName"], $_POST["catDescription"], $_FILES["catImage"])){
	header('Location: category_add.php');
}

 

However, since you are using a function, it makes not since to me to have that line. If I am reading it correctly, you might want to change it to something like this:

 

function addCategory(){
if(isset($_POST['catSubmit'])) {
$name = $_POST["catName"];
$description = $_POST["catDescription"];
$image = $_FILES["catImage"]["name"];
$image = addImage($image, SRV_ROOT);	 // FIX FIX FIX
$parentId = $_POST["parentCat"];
if(isset($_POST["isProduct"])){
	$isProduct = 1;
}else{
	$isProduct = 0;	
}

$sql = "INSERT INTO catagory_tbl (cat_parent_id, cat_isProduct, cat_name, cat_description, cat_thumbnail) VALUES ($parentId, $isProduct, '$name', '$description', '$image')";
$result = dbQuery($sql);
header('Location: category_list.php');
}
}

 

I am not sure if the product part will work, but you can try this code out. If it doesn't work, then take it out of the function completely and then try it to see if something gets posted to the database.

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.