Uploading using a relative path - SOLVED

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Uploading using a relative path - SOLVED

Post by psurrena »

My project will have an admin area which is in its own directory. One part of that is the fact you can upload images. I want the images to upload to outside of the admin directory.

When I use the relative path "../path" It just says "error upploading".

When I put the whole path for the site I get

"Warning: move_uploaded_file(images/article/390601_rotary_art_01.jpg): failed to open stream: No such file or directory in /webroot/e/n/enume001/www/testing/test-blog/admin/add-image.php on line 19

Warning: move_uploaded_file(): Unable to move '/tmp/phpuploads/phpLtloPf' to 'images/article/390601_rotary_art_01.jpg' in /webroot/e/n/enume001/www/testing/test-blog/admin/add-image.php on line 19
Error uploading file"

Code: Select all

<?php
	include '../includes/connect.php';

	$a_id = $_GET['a_id'];
	
	$uploadDir = '../images/article/';			
	
	if(isset($_POST['upload'])) {
		$fileName		= $_FILES['userfile']['name'];
		$tmpName  		= $_FILES['userfile']['tmp_name'];
		$fileSize 		= $_FILES['userfile']['size'];
		$fileType 		= $_FILES['userfile']['type'];
		

		$filePath 	    = 'images/article/' . $fileName;
		$result 		= move_uploaded_file($tmpName, $filePath);
			
		if (!$result) {
			echo "Error uploading file";
			exit;
		}
	
		if(!get_magic_quotes_gpc()) {
			$fileName  = addslashes($fileName);
			$filePath  = addslashes($filePath);
		}  
	
		$query = "INSERT INTO image (name, size, type, path, a_id)" . "VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '$a_id')";
	
		mysql_query($query) or die('Error, query failed: ' . mysql_error());  
		
		mysql_close();
		
		header ('location:index.php');
	}        
?>
Last edited by psurrena on Thu Aug 31, 2006 9:27 am, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

psurrena wrote:"Warning: move_uploaded_file(images/article/390601_rotary_art_01.jpg): failed to open stream: No such file or directory in /webroot/e/n/enume001/www/testing/test-blog/admin/add-image.php on line 19
I don't see any .. in the path.


What does

Code: Select all

echo 'file: ', __FILE__, "<br />\ncwd: ", getcwd(), "<br />\n";
$result = move_uploaded_file($tmpName, $filePath);
report?
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post by psurrena »

file: /webroot/e/n/enume001/www/testing/test-blog/admin/add-image.php
cwd: /webroot/e/n/enume001/www/testing/test-blog/admin
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

cwd + relative path -> absoulte path
Does the directory /webroot/e/n/enume001/www/testing/test-blog/admin/images/article/ exist?
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Uploading using a relative path - SOLVED

Post by psurrena »

It worked great. In addition all I did was add another variable for the move_uploaded_file() function.

Thanks.

Code: Select all

<?php
	include '../includes/connect.php';

	$a_id = $_GET['a_id'];
	
	$uploadDir = '/webroot/e/n/long-address....';			
	
	if(isset($_POST['upload'])) {
		$fileName		= $_FILES['userfile']['name'];
		$tmpName  		= $_FILES['userfile']['tmp_name'];
		$fileSize 		= $_FILES['userfile']['size'];
		$fileType 		= $_FILES['userfile']['type'];
		
		$fullPath 	    = $uploadDir . $fileName;
		$filePath		= 'images/article/' . $fileName; 

		/*echo 'file: ', __FILE__, "<br />\ncwd: ", getcwd(), "<br />\n";*/
		
		$result 		= move_uploaded_file($tmpName, $fullPath);
			
		if (!$result) {
			echo "Error uploading file";
			exit;
		}
	
		if(!get_magic_quotes_gpc()) {
			$fileName  = addslashes($fileName);
			$filePath  = addslashes($filePath);
		}  
	
		$query = "INSERT INTO image (name, size, type, path, a_id)" . "VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '$a_id')";
	
		mysql_query($query) or die('Error, query failed: ' . mysql_error());  
		
		mysql_close();
		
		header ('location:index.php');
	}        
?>
Post Reply