0

I want to upload file with php(text file) and always this file saved as names.txt(if the file exists from previous upload i want to be replaced with the new one) to the same directory the script is located. i found the below code online from a tutorial but i can't make it work .I get no file exists. I have posted the html file and the php file from upload to help me. thank you uploadfile.html

<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="file_uploader.php" method="post"
                        enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>

file_uploader.php

<?php
if( $_FILES['file']['name'] != "" )
{
   copy( $_FILES['file']['name'], "/uploads" ) or 
           die( "Could not copy file!");
}
else
{
    die("No file specified!");
}
?>
<html>
<head>
<title>Uploading Complete</title>
</head>
<body>
<h2>Uploaded File Info:</h2>
<ul>
<li>Sent file: <?php echo $_FILES['file']['name'];  ?>
<li>File size: <?php echo $_FILES['file']['size'];  ?> bytes
<li>File type: <?php echo $_FILES['file']['type'];  ?>
</ul>
</body>
</html>

2 Answers 2

3

Use move_uploaded_file() instead of copy(). If the destination file already exists, it will be overwritten.

Your destination is invalid. You are passing a directory to destination file.

copy( $_FILES['file']['tmp_name'], "/uploads/".$_FILES['file']['name'] );
Sign up to request clarification or add additional context in comments.

5 Comments

i tried to use move_uploaded_file() but i get the "No file specified!"
I also tried the code you gave me later and i get this "Parse error: syntax error, unexpected T_LOGICAL_OR in /Applications/XAMPP/xamppfiles/htdocs/file_uploader.php on line 4"
You copied all of the line with ";" to in if statement ?
i saw that and i removed it . after that i i am still getting "No file specified"
i tried it on an other server and it worked...i had a problem with the permissions....thank you
0

for uploading file i use the code like as below

<?php
if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  echo "Type: " . $_FILES["file"]["type"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  } 
  ?> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.