Upload error

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
nen29
Forum Newbie
Posts: 4
Joined: Sun Apr 26, 2009 4:47 pm

Upload error

Post by nen29 »

Hi I'm having problems uploading a file to a server running vista, I would be grateful for any help. Sorry if this has been answered before.

The errors I'm getting are:

[error] [client 192.168.1.50] PHP Warning: move_uploaded_file(C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\Applet) [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: failed to open stream: Permission denied in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\upload.php on line 7

[error] [client 192.168.1.50] PHP Warning: move_uploaded_file() [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: Unable to move 'C:\\Windows\\Temp\\phpDECE.tmp' to 'C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\Applet' in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\upload.php on line 7

Code: Select all

<?php
 
$target_path = "C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\Applet";
 
$target_path = $target_path.basename($_FILES['uploadedfile']['name']);
 
if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$target_path))
{
     echo "The file ".basename($_FILES['uploadedfile']['name'])." has been uploaded";
}
     else
{
     echo "There was an error uploading the file, please try again!";
}
?>
Last edited by Benjamin on Tue Apr 28, 2009 1:34 pm, edited 1 time in total.
Reason: Changed code type from text to php.
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Re: Upload error

Post by ben.artiss »

I came across a similar problem a while ago, I only managed to fix it by deleting the directory and making it again through the script using mkdir. But if you have the chmod function available try changing the permissions of the dir to 666 (for windows I think), or else 0777. Hope that helps a bit.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Upload error

Post by McInfo »

Install Apache somewhere other than Program Files, like C:\apache.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 12:51 pm, edited 1 time in total.
theskyeagle
Forum Newbie
Posts: 1
Joined: Sun Apr 26, 2009 7:35 pm

Re: Upload error

Post by theskyeagle »

Try this:

Code: Select all

<?
if(is_file($_FILES['uploadedfile']['tmp_name'])){
    if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$target_path))
    {
      echo "The file ".basename($_FILES['uploadedfile']['name'])." has been uploaded";
    }
    else
    {
      echo "There was an error uploading the file, please try again!";
    }
}else{
    echo "There was an error uploading the file, please try again!";
}
?>
Last edited by Benjamin on Tue Apr 28, 2009 1:35 pm, edited 1 time in total.
Reason: Changed code type from text to php.
nen29
Forum Newbie
Posts: 4
Joined: Sun Apr 26, 2009 4:47 pm

Re: Upload error

Post by nen29 »

I tried installing apache in c:\apache but I'm still getting the same error. Is there anything else I can try? I really need to get this working this week
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Upload error

Post by McInfo »

What is the output of this script after you upload a file with it?

file-upload-debugger.php

Code: Select all

<p>File uploads is <?php echo ini_get('file_uploads'); ?></p>
<p>Upload attempts: <?php echo ($attempts = isset($_POST['attempts']) ? $_POST['attempts'] + 1 : 0); ?></p>
<form method="post" action="file-upload-debugger.php" enctype="multipart/form-data">
    <input type="hidden" name="attempts" value="<?php echo $attempts; ?>" />
    <input type="file" name="uploadedFile" />
    <input type="submit" value="Upload" />
</form>
<pre>$_FILES => <?php print_r($_FILES); ?></pre>
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 12:52 pm, edited 1 time in total.
nen29
Forum Newbie
Posts: 4
Joined: Sun Apr 26, 2009 4:47 pm

Re: Upload error

Post by nen29 »

McInfo wrote:What is the output of this script after you upload a file with it?

file-upload-debugger.php

Code: Select all

<p>File uploads is <?php echo ini_get('file_uploads'); ?></p>
<p>Upload attempts: <?php echo ($attempts = isset($_POST['attempts']) ? $_POST['attempts'] + 1 : 0); ?></p>
<form method="post" action="file-upload-debugger.php" enctype="multipart/form-data">
    <input type="hidden" name="attempts" value="<?php echo $attempts; ?>" />
    <input type="file" name="uploadedFile" />
    <input type="submit" value="Upload" />
</form>
<pre>$_FILES => <?php print_r($_FILES); ?></pre>
$_FILES => Array
(
[uploadedFile] => Array
(
[name] => test.txt
[type] => text/plain
[tmp_name] => C:\Windows\Temp\php1451.tmp
[error] => 0
[size] => 15
)

)
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Upload error

Post by McInfo »

Another script for you to try...

file-upload-debugger-2.php

Code: Select all

<p>Warning: An uploaded file will overwrite an existing file with the same name if one exists.</p>
<form method="post" action="file-upload-debugger-2.php" enctype="multipart/form-data">
    <input type="file" name="uploadedFile" />
    <input type="submit" value="Upload" />
</form>
<pre><?php
echo '$_FILES => ';
print_r($_FILES);
if (!empty($_FILES)) {
    $tmp_name =& $_FILES['uploadedFile']['tmp_name'];
    if (is_file($tmp_name)) {
        echo 'Found temporary file at '.$tmp_name."\n";
        $name =& $_FILES['uploadedFile']['name'];
        $dest = './'.$name;
        if (move_uploaded_file($tmp_name, $dest)) {
            echo 'File successfully moved to '.$dest."\n";
        } else {
            echo 'Failed to move file to '.$dest."\n";
            $dest = getcwd().'/'.$name;
            if (move_uploaded_file($tmp_name, $dest)) {
                echo 'File successfully moved to '.$dest."\n";
            } else {
                echo 'Failed to move file to '.$dest."\n";
            }
        }
    } else {
        echo 'Cannot find temporary file at '.$tmp_name."\n";
    }
}
?></pre>
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 12:53 pm, edited 1 time in total.
nen29
Forum Newbie
Posts: 4
Joined: Sun Apr 26, 2009 4:47 pm

Re: Upload errorhttp://forums.devnetwork.net/posting.php?mode=re

Post by nen29 »

Hi thanks for the help

The output I get is -

Code: Select all

 
$_FILES => Array
(
    [uploadedFile] => Array
        (
            [name] => test.txt
            [type] => text/plain
            [tmp_name] => C:\Windows\Temp\php3015.tmp
            [error] => 0
            [size] => 15
        )
 
)
 
Found temporary file at C:\Windows\Temp\php3015.tmp
File successfully moved to ./test.txt

The file moved correctly and everything seems to work. The problem now is I need to upload the file through a java program, how would I edit the script to allow this?

Thanks for you help so far.
Last edited by Benjamin on Tue Apr 28, 2009 1:35 pm, edited 2 times in total.
Reason: Added code tags.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Upload error

Post by McInfo »

A Java applet should be able to send a POST request to your PHP script.

If you dig through the source code of JUpload, you can find an example.

Edit: This post was recovered from search engine cache.
Post Reply