Jump to content

Recommended Posts

Hi.

These are the codes that I'm using:

 

upload.html

<html>
<head>
  <title>Administration - upload new files</title>
</head>
<body>
<h1>Upload new news files</h1>
<form enctype="multipart/form-data" action="upload.php" method=post>
  <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
  Upload this file: <input name="userfile" type="file">
  <input type="submit" value="Send File">
</form>
</body>
</html>

 

upload.php

<html>
<head>
  <title>Uploading...</title>
</head>
<body>
<h1>Uploading file...</h1>
<?php

//Check to see if an error code was generated on the upload attempt
  if ($_FILES['userfile']['error'] > 0)
  {
    echo 'Problem: ';
    switch ($_FILES['userfile']['error'])
    {
      case 1:	echo 'File exceeded upload_max_filesize';
  			break;
      case 2:	echo 'File exceeded max_file_size';
  			break;
      case 3:	echo 'File only partially uploaded';
  			break;
      case 4:	echo 'No file uploaded';
  			break;
  case 6:   echo 'Cannot upload file: No temp directory specified.';
  			break;
  case 7:   echo 'Upload failed: Cannot write to disk.';
  			break;
    }
    exit;
  }

  // Does the file have the right MIME type?
  if ($_FILES['userfile']['type'] != 'text/plain')
  {
    echo 'Problem: file is not plain text';
    exit;
  }

  // put the file where we'd like it
  $upfile = '/uploads/'.$_FILES['userfile']['name'];

  if (is_uploaded_file($_FILES['userfile']['tmp_name'])) 
  {
     if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile))
     {
        echo 'Problem: Could not move file to destination directory';
        exit;
     }
  } 
  else 
  {
    echo 'Problem: Possible file upload attack. Filename: ';
    echo $_FILES['userfile']['name'];
    exit;
  }


  echo 'File uploaded successfully<br><br>'; 

  // reformat the file contents
  $fp = fopen($upfile, 'r');
  $contents = fread ($fp, filesize ($upfile));
  fclose ($fp);

  $contents = strip_tags($contents);
  $fp = fopen($upfile, 'w');
  fwrite($fp, $contents);
  fclose($fp);

  // show what was uploaded
  echo 'Preview of uploaded file contents:<br><hr>';
  echo $contents;
  echo '<br><hr>';

?>
</body>
</html>

 

 

I'm getting this error when trying to upload helloSampleFile.txt:

 

Warning: move_uploaded_file(/uploads/helloSampleFile.txt) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\xampp\htdocs\learning\upload.php on line 43

 

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\xampp\tmp\php196C.tmp' to '/uploads/helloSampleFile.txt' in C:\xampp\htdocs\learning\upload.php on line 43

Problem: Could not move file to destination directory

 

 

Line 43 is:

 

if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile))

 

 

My DocumentRoot is "C:\xampp\htdocs". I also made a folder "uploads" in this directory.

 

Does anyone know what I'm doing wrong and how I should fix this? Thanks.

Link to comment
https://forums.phpfreaks.com/topic/263124-uploading-file/
Share on other sites

I'm trying to move the file, which is now temporarily stored on the webserver, to the /uploads/ directory since in the end, the temporary file will be deleted.

 

I'm really confused about this first error (which might be causing the second error):

Warning: move_uploaded_file(/uploads/helloSampleFile.txt) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\xampp\htdocs\learning\upload.php on line 43

 

How does this line

if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile)) in the context of

 

  $upfile = '/uploads/'.$_FILES['userfile']['name'];

  if (is_uploaded_file($_FILES['userfile']['tmp_name'])) 
  {
     if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile))
     {
        echo 'Problem: Could not move file to destination directory';
        exit;
     }
  } 

 

give us this error?

Link to comment
https://forums.phpfreaks.com/topic/263124-uploading-file/#findComment-1348807
Share on other sites

Okay. I found that my ServerRoot is "C:/xampp/apache," while my DocumentRoot is "C:/xampp/htdocs" so I made a folder called "uploads" in the ServerRoot, but it still didn't work. I also tried taking out '/uploads/'. from this line:

$upfile = '/uploads/'.$_FILES['userfile']['name'];.

When I did this, no error showed up and I got to the page that shows what the text file contains. However, I don't know where the file is stored, if it is stored at all.

 

Any help will really be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/263124-uploading-file/#findComment-1348879
Share on other sites

Hello, I'm surprised no-ones tried to help you with this.

 

You need to ensure both directories are correct. The tmp location/file should be correct as its generated by the server. Your storing the file in the RAM of the server at this point and you now need to move it to a permanent location on the hard disk of the server; hence "move_uploaded_file".

 

Your also using a slash at the front of your $upfile variable which means your attempting to upload the file to some-where related to the root location.

 

For testing purposes, try doing:

 

$upfile = "C:/xampp/htdocs/".$_FILES['userfile']['name'];

 

And see if it moves the file...

Link to comment
https://forums.phpfreaks.com/topic/263124-uploading-file/#findComment-1348894
Share on other sites

It worked! Thank you so much! It brought me to the correct page, and the text file is stored in the "C:/xampp/htdocs" directory.

 

I'm curious as to why my initial approach didn't work. If I used:

 

$upfile = '/uploads/'.$_FILES['userfile']['name'];

 

shouldn't the file be moved to the uploads folder in the ServerRoot?

Link to comment
https://forums.phpfreaks.com/topic/263124-uploading-file/#findComment-1348947
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.