Heya,
I'm trying to write a routine that will allow users to upload a single image to the server. At the moment I am designing the site to use with localhost. Here is the script I am using:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="fileForm" enctype="multipart/form-data">
File to upload:
<table>
<tr><td><input name="upfile" type="file"></td></tr>
<tr><td><input type="submit" name="submitBtn" value="Upload"></td></tr>
</table>
</form>
<?php
if (isset($_POST['submitBtn'])){
// Define the upload location
$target_path = "http:\\localhost\sitename\uploads";
// Create the file name with path
$target_path = $target_path . basename( $_FILES['upfile']['name']);
// Try to move the file from the temporay directory to the defined.
if(move_uploaded_file($_FILES['upfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['upfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
?>
The problem is that it seems to have a problem uploading to the root directory. I have changed the upload directory to one on my PC (c:\program files\ for example) and it works great doing that, the images are uploaded successfully. However, as soon as I try to upload into the root directory of the server, I get the following error:
Warning: move_uploaded_file(C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\sitename\7041.jpg) [function.move-uploaded-file]: failed to open stream: Invalid argument in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\sitename\upload.php on line 27
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\WINDOWS\TEMP\phpA0.tmp' to 'C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\sitename\7041.jpg' in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\sitename\upload.php on line 27
There was an error uploading the file, please try again!
Should I not be uploading to the root directory? If thats not a problem, then whats going wrong!?
Thanks a lot