Jump to content

Uploading a file


ameliabob
 Share

Recommended Posts

I am trying to upload a simple text file from the browser to the server. I have used the example in the w3schools but I still get the "Sorry, there was an error uploading your file." message.

How do I find out what the error is?

 

The code is as follows:

<codebox><?php$target_dir = "E:/wamp/www/profitsrun/";$target_file = "alert.txt";$uploadOk = 1;$upload_dir = "c:/profitsrun/";$upload_file = $upload_dir.$target_file;echo($target_file."<br>");echo("file_to_upload is ".$upload_file)."<br>";echo("it will be put in ".$target_dir.$target_file."<br>");// Check if file already existsif (file_exists($target_dir.$target_file)) {      echo($target_dir.$target_file);      echo "Sorry, file already exists.";      $uploadOk = 0;}// Check if $uploadOk is set to 0 by an errorif ($uploadOk == 0) {      echo "Sorry, your file was not uploaded.";  // if everything is ok, try to upload file} else {      if (move_uploaded_file($upload_file, $target_dir.$target_file)) {            echo "The file ". $upload_file. " has been uploaded.";      } else {            echo "Sorry, there was an error uploading your file.";      }}?></codebox>
Link to comment
Share on other sites

Make sure that all error messages are enabled:

ini_set('display_errors', 1);error_reporting(E_ALL);
One problem in that code is that you are not working with an uploaded file. Files uploaded through a form are in the $_FILES array, you have the name just hard-coded. If you want to work with an existing file then use the copy function instead of move_uploaded_file.
Link to comment
Share on other sites

Make sure that all error messages are enabled:

ini_set('display_errors', 1);error_reporting(E_ALL);
One problem in that code is that you are not working with an uploaded file. Files uploaded through a form are in the $_FILES array, you have the name just hard-coded. If you want to work with an existing file then use the copy function instead of move_uploaded_file.

 

I am having trouble with the fact that I am doing the browser to server always with the same file. I do not need the flexibility to change the file names. How would I code the "copy" functioin?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...