Jump to content

PHP File upload - directory error


theonewhotopes

Recommended Posts

 

Hey,

 

I am building a website where I want to let users upload files such as pdf and images, but I am having trouble getting my upload script to work. My main objective is to store the files in a separate directory, and to have the filepath, etc. in the database. I found a tutorial with the following script, which I modified to fit my needs, but whenever I try to upload a file, it always gives the same error message:

 

Warning: move_uploaded_file(../www/www/upload_files/2d9b80912563d5cef1593e1a2871b866.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/ts/www/www/upload.php on line 71

 

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpsuOZ3a' to '../www/www/upload_tests/2d9b80912563d5cef1593e1a2871b866.jpg' in /home/ts/www/www/upload.php on line 71

Error uploading file

 

So far, I have tried switching the upload directory around and playing with the privileges of the database, but to no avail. Beyond that, I am pretty lost, as I am new to php. Here is the code I have been using:

 

<?
$uploadDir = '/upload_files/';


if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

    // get the file extension first
$ext      = substr(strrchr($fileName, "."), 1); 

// generate the random file name
$randName = md5(rand() * time());

// and now we have the unique file name for the upload file
    $filePath = $uploadDir . $randName . '.' . $ext;

    // move the files to the specified directory
// if the upload directory is not writable or
// something else went wrong $result will be false
    $result    = move_uploaded_file($tmpName, $filePath);   //ERROR is here
if (!$result) {
	echo "Error uploading file";
	exit;
}

    include 'library/config.php'; //contains database connect info
    include 'library/opendb.php'; //connects to database

    if(!get_magic_quotes_gpc())
    {
        $fileName  = addslashes($fileName);
        $filePath  = addslashes($filePath);
    }  

$query = "INSERT INTO upload (name, size, type, path ) ".
		 "VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";

    mysql_query($query) or die('Error, query failed : ' . mysql_error());                    

    include 'library/closedb.php'; //ends connection
    
    echo "<br>File uploaded<br>";
}		
?>
<!--upload form-->
<form action="" method="post" enctype="multipart/form-data" name="uploadform">
  <table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
    <tr> 
      <td width="246"><input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="userfile" type="file" class="box" id="userfile">
	 </td>
      <td width="80"><input name="upload" type="submit" class="box" id="upload" value="  Upload  "></td>
    </tr>
  </table>
</form>

 

Here's the create table code:

 

CREATE TABLE upload (

id INT NOT NULL AUTO_INCREMENT,

name VARCHAR(30) NOT NULL,

type VARCHAR(30) NOT NULL,

size INT NOT NULL,

path VARCHAR(60) NOT NULL,

PRIMARY KEY(id)

);

 

I am currently running PHP v 5.2.1 50201 and MySQL v5.0.37

 

Any help will be greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/48320-php-file-upload-directory-error/
Share on other sites

Solved it--turns out that my directory was wrong; entered the right one and now everything works great.

 

Ok, so I shouldn't have been so hasty...

 

This upload script works fine, but when I try to download the file using the following script, the file download says that it is starting, but the file I get is corrupted (for example for a 1 mb .doc file that I uploaded, I recieved a 266 byte .doc file). When I try to use my host's file manager to download the file, it says that I don't have permission to access the file.

 

Here's my download code:

 

<?php
error_reporting(E_ALL);
if(isset($_GET['id']))
{
include 'library/config.php';
include 'library/opendb.php';

$id      = $_GET['id'];
$query   = "SELECT name, type, size, path FROM upload WHERE id = '$id'";
$result  = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $filePath) = mysql_fetch_array($result);

header("Content-Disposition: attachment; filename=$name");
header("Content-length: $size");
header("Content-type: $type");

readfile($filePath);

include 'library/closedb.php';	
exit;
}

?>

<html>
<head>
<title>Download File From File Server</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
include 'library/config.php';
include 'library/opendb.php';

$query  = "SELECT id, name FROM upload";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "Database is empty <br>";
} 
else
{
while(list($id, $name) = mysql_fetch_array($result))
{
?>
<a href="download.php?id=<?=$id;?>"><?=$name;?></a> <br>
<?php		
}
}
include 'library/closedb.php';
?>
</body>
</html>

 

Again, any help would be greatly appreciated...

 

 

sir , where can i find the uploaded files/images.

 

moreover  $uploadDir='\upload_files'\ ; is correct

 

i can run script successfully without errors,but where does these uploaded files going???/ plz i am also new to php......

 

how to post messages thru my id???

 

 

-----------------------------------------

<?php

$uploadDir='\upload_files'\ ;

if(isset($_POST[upload]))

{

$fileName=$_FILES['userfile']['name'];

$tmpName=$_FILES['userfile']['tmp_name'];

$fileSize=$_FILES['username']['size'];

$fileType=$_FILES['username']['type'];

// get the extension

$ext=substr($fileName,strpos($fileName, "."),1);

//get the random file

$randName=md5(rand() * time());

$filePath=$uploadDir . $randName . '.' . $ext;

$resul    = move_uploaded_file($tmpName, $filePath);  //ERROR is here

echo $resul;

if (!$resul) {

echo "Error uploading file";

exit;

}

if(!get_magic_quotes_gpc())

    {

        $fileName  = addslashes($fileName);

        $filePath  = addslashes($filePath);

    } 

          $my=mysql_connect("localhost","root","");

$s=mysql_select_db("leela",$my);

$query = "INSERT INTO upload (name, size, type, path ) ".

"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";

 

    mysql_query($query) or die('Error, query failed : ' . mysql_error());                   

 

}

?>

<!--upload form-->

<form action="" method="post" enctype="multipart/form-data" name="uploadform">

  <table width="350" border="0" cellpadding="1" cellspacing="1" class="box">

    <tr>

      <td width="246"><input type="hidden" name="MAX_FILE_SIZE" value="2000000">

  <input name="userfile" type="file" class="box" id="userfile">

</td>

      <td width="80">

  <input name="upload" type="submit" class="box" id="upload" value="  Upload  "></td>

    </tr>

  </table>

</form>

 

 

--------------------------------------

thanks,

lee

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.