user1557515
user1557515

Reputation: 109

uploading files to web directory with PHP

I am trying to make a simple uploading application from a web page:localhost/test.html. I am getting these errors:

Warning: move_uploaded_file(test/Blue hills.jpg): failed to open stream: No such file or directory in C:\wamp\www\test.html on line 11

and

Warning: move_uploaded_file(): Unable to move 'C:\wamp\tmp\php376.tmp' to 'test/Blue hills.jpg' in C:\wamp\www\test.html on line 11

Here is my code

<html>
  <form enctype="multipart/form-data" action="test.html" method="POST">
    Please choose a file: <input name="uploaded" type="file" /><br />
    <input type="submit" value="Upload" />
 </form>
<html>

<?php

$target = "test/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;

if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {
  echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} else {
  echo "Uploading Error.";
}

Upvotes: 1

Views: 896

Answers (3)

Popnoodles
Popnoodles

Reputation: 28409

Probably the directory test doesn't exist. Add these lines to your code.

if (file_exists('test/')) echo 'Ok it wasn\'t that';
else echo 'Um, create a directory called test here: '.dirname(__FILE__);

Upvotes: 2

Stichoza
Stichoza

Reputation: 4609

Change directory permissions (CHMOD) to 777 via your FTP client (read,write,execute for owner,group,public).

Upvotes: 0

Raffaele
Raffaele

Reputation: 20875

Ensure that the a test/ directory exists in the directory where your script is located, then you can use

$out = dirname(__FILE__) . '/' . $_FILES['uploaded']['name'];
move_uploaded_file($_FILES['uploaded']['tmp_name'], $out);

Upvotes: 0

Related Questions