Jump to content

file handling issues


ra_ie_darkness

Recommended Posts

I'm using xampp on windows vista

I am a beginner in php and am working on creating a file hosting website

got a form to upload the file from the client in uploadform.php

<html>
<body>
<form method="post" action="uploadfile.php" enctype="multipart/form-data">
<input type="file" name="usr" id="usr"/><br />
<input type="submit" value="Upload"/>
</form>
</body>
</html>

 

the file handling code is in uploadfile.php

<?php
class upload
{ 
    
    function fupload()
    {
        $dir = "C:\\xampp\htdocs\learning\fileshare database.txt";
        
        echo "My name is: ".$_FILES['usr']['name']."<br/>";
        echo "Location ".$_FILES['usr']['tmp_name']."<br/>";
        echo "Size:".($_FILES['usr']['size'] / 1024)." kb <br/>";
        echo "File type".$_FILES['usr']['type']."<br/>";
        
        if(file_exists("htdocs/".$_FILES['usr']['name']))
        {
            echo $_FILES['usr']['name']."already exists";
        }
        else
        {
            copy($_FILES['usr']['name'],$dir);
            echo "File moved";
        }
        
        
        
    }
    
}

$up = new upload();
$up->fupload();
?>

 

 

The problem is that it does not copy the file from the tmp directory to the specified one

the error i get is:

Warning: copy(fileshare databse.txt) [function.copy]: failed to open stream: No such file or directory in C:\xampp\htdocs\learning\uploadfile.php on line 20

 

I am facing the same problem if i try move_uploaded_file () function

Could any one help with this issue, thanks

Link to comment
Share on other sites

the error i get is:

Warning: copy(fileshare databse.txt) [function.copy]: failed to open stream: No such file or directory in C:\xampp\htdocs\learning\uploadfile.php on line 20

 

Your code specifically defines $dir as $dir = "C:\\xampp\htdocs\learning\fileshare database.txt".

If you're receiving that error, then you need to change the spelling in your file name.

Link to comment
Share on other sites

copy($_FILES['usr']['name'],$dir);

 

The name element of the FILES array is the basename (filename without the path) of the file that the user uploaded. The tmp_name element is the filename the server used to temporarily store the uploaded file. This temporary file is stored in a directory somewhere on the server (not in your path). The move_uploaded_file function knows where this file is stored, which is one of the reasons you should use that function instead of copy.

 

The chances are very very high that the tmp_name is not the same as the name.

 

Link to comment
Share on other sites

okay i am now using move_uploaded_file

here is how it looks like

<?php
class upload
{ 
    
    function fupload()
    {
        
        echo "My name is: ".$_FILES['usr']['name']."<br/>";
        echo "Location ".$_FILES['usr']['tmp_name']."<br/>";
        echo "Size:".($_FILES['usr']['size'] / 1024)." kb <br/>";
        echo "File type".$_FILES['usr']['type']."<br/>";
        
        if(file_exists("htdocs/".$_FILES['usr']['name']))
        {
            echo $_FILES['usr']['name']."already exists";
        }
        else
        {
            move_uploaded_file($_FILES['usr']['tmp_name'],"/learning/".$_FILES['usr']['name']);
            echo "File moved";
        }
        
        
        
    }
    
}

$up = new upload();
$up->fupload();
?>

is it possible that i am not using the function properly?

learning is a folder inside htdocs(xampp)

now I am getting this message

 

Warning: move_uploaded_file(/learning/) [function.move-uploaded-file]: failed to open stream: Invalid argument in C:\xampp\htdocs\learning\uploadfile.php on line 19

 

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\xampp\tmp\phpA56E.tmp' to '/learning/' in C:\xampp\htdocs\learning\uploadfile.php on line 19

Link to comment
Share on other sites

move_uploaded_file($_FILES['usr']['tmp_name'],"/learning/".$_FILES['usr']['name']);

 

The leading slash on your destination "/learning/" indicates the root of the file system. This would be equivalent to specifying "C:\learning\...". This is most likely NOT where you are trying to put the file. The error message is indicating that that directory does not exist.

 

You need to either take off the leading slash --- which will put it in the "current" directory, wherever that is --- OR (the preferred approach) define a variable that holds the destination directory name:

 

Using parts from your original post:

        $dir = "C:\\xampp\\htdocs\\learning\\";
// ... checking and processing code
move_uploaded_file($_FILES['usr']['tmp_name'], $dir . $_FILES['usr']['name']);

 

Link to comment
Share on other sites

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.