2 Replies - 296 Views - Last Post: 05 April 2011 - 10:24 PM Rate Topic: -----

#1 jagadish_c  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 31
  • Joined: 10-January 11

Unable to upload image

Posted 05 April 2011 - 10:05 PM

Hi all. I used one of the tutorial on this forum "Uploading Images With PHP" posted by Akozlik for uploading images onto database and to fetch it for displaying. The code is working properly when executed on localhost. But it is not working i.e., the image is not getting uploaded when the code is executed on server. I was suggested by the author of the tutorial to check the file permission of the folder to which I am uploading to. But still the problem is not solved.

I am getting the error message "Could not upload file. Check read/write persmissions on the directory" from the below part of code when the move_uploaded_file() function is called at the line 75.

<?php
// Start a session for error reporting
session_start();

// Call our connection file
require("includes/conn.php");

// Check to see if the type of file uploaded is a valid image type
function is_valid_type($file)
{
	// This is an array that holds all the valid image MIME types
	$valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif");

	if (in_array($file['type'], $valid_types))
		return 1;
	return 0;
}

// Just a short function that prints out the contents of an array in a manner that's easy to read
// I used this function during debugging but it serves no purpose at run time for this example
function showContents($array)
{
	echo "<pre>";
	print_r($array);
	echo "</pre>";
}

// Set some constants

// This variable is the path to the image folder where all the images are going to be stored
// Note that there is a trailing forward slash
$TARGET_PATH = "images/";

// Get our POSTed variables
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$image = $_FILES['image'];

// Sanitize our inputs
$fname = mysql_real_escape_string($fname);
$lname = mysql_real_escape_string($lname);
$image['name'] = mysql_real_escape_string($image['name']);

// Build our target path full string.  This is where the file will be moved do
// i.e.  images/picture.jpg
$TARGET_PATH .= $image['name'];

// Make sure all the fields from the form have inputs
if ( $fname == "" || $lname == "" || $image['name'] == "" )
{
	$_SESSION['error'] = "All fields are required";
	header("Location: index.php");
	exit;
}

// Check to make sure that our file is actually an image
// You check the file type instead of the extension because the extension can easily be faked
if (!is_valid_type($image))
{
	$_SESSION['error'] = "You must upload a jpeg, gif, or bmp";
	header("Location: index.php");
	exit;
}

// Here we check to see if a file with that name already exists
// You could get past filename problems by appending a timestamp to the filename and then continuing
if (file_exists($TARGET_PATH))
{
	$_SESSION['error'] = "A file with that name already exists";
	header("Location: index.php");
	exit;
}

// Lets attempt to move the file from its temporary directory to its new home
if (move_uploaded_file($image['tmp_name'], $TARGET_PATH))
{
	// NOTE: This is where a lot of people make mistakes.
	// We are *not* putting the image into the database; we are putting a reference to the file's location on the server
	$sql = "insert into people (fname, lname, filename) values ('$fname', '$lname', '" . $image['name'] . "')";
	$result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());
	header("Location: images.php");
	exit;
}
else
{
	// A common cause of file moving failures is because of bad permissions on the directory attempting to be written to
	// Make sure you chmod the directory to be writeable
	$_SESSION['error'] = "Could not upload file.  Check read/write persmissions on the directory";
	header("Location: index.php");
	exit;
}
?>





Can anybody help me with this?

Is This A Good Question/Topic? 0
  • +

Replies To: Unable to upload image

#2 maffelu  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 185
  • Joined: 21-August 08

Re: Unable to upload image

Posted 05 April 2011 - 10:17 PM

Well, it sounds pretty much like you should check your permissions. Run chmod 777 /uploadFolder.
Was This Post Helpful? 0
  • +
  • -

#3 jagadish_c  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 31
  • Joined: 10-January 11

Re: Unable to upload image

Posted 05 April 2011 - 10:24 PM

View Postmaffelu, on 05 April 2011 - 10:17 PM, said:

Well, it sounds pretty much like you should check your permissions. Run chmod 777 /uploadFolder.

Thanks for your quick reply. I am using windows XP OS. And the file permissions have been set to read, write and execute already. But still I am getting the error.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1