Jump to content

Recommended Posts

Hello there,

 

I have been working on getting an index.php page set up to show the list of files in a specified folder.  Now I am trying to get an upload files option to go into that same folder.  Then if you hit refresh, the new file that was "successfully" uploaded will be in that list.  Here's the code I have to generate the list.  It works great.

 

<?php
if ($handle = opendir('.')) {
   $ignore_files = array('.', '..', '.htaccess', '.htpasswd', 'index.php');
   while (false !== ($file = readdir($handle)))
      {
          if (!in_array($file, $ignore_files))
     {
             $thelist .= '<a href="'.$file.'">'.$file.'</a>'.'<br>';
          }
       }
  closedir($handle);
  }
?>
<p style="font-weight: bold; font-size: 10pt">List of files:</p>
<p style="font-weight: bold; font-size: 10pt; color: #560D1C"><?=$thelist?></p>

 

Now I need to add more code to upload files.  Thanks,

 

Lorne

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/119561-upload-files-to-same-folder-as-indexphp/
Share on other sites

Well while you guys crack yourselves up...I tried some of the tutorials and I can't get it to work.  Why, hell...I don't know why?  That's why I'm posting here.

 

If you're willing to help great.  If you're not, then don't say anything, cuz it's pretty annoying.  I do appreciate any help anyone can give.  I'm sorry i'm a beginner at this PHP stuff and I don't mean to ask dumb questions.  So please bear with me.

 

This is the code I put in my index.php page:

<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

 

This is the code I put in my uploader.php page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
// Where the file is going to be placed 
$target_path = "/";

/* Add the original filename to our target path.  
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
$_FILES['uploadedfile']['tmp_name'];

$target_path = "/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}

?>
</body>
</html>

 

Whenever I try to upload a file I get the echo message: "There was an error uploading the file, please try again!"

Does anyone know why this is happening?  What am I missing?

 

Thanks in advance,

 

Lorne

The problem is likely to do with this...

 

$target_path = "/";

 

This will make your target directory the root of your servers filesystem, it is highly unlikely apache has permission to write anything there.

 

Place the following at the top of your script to see if we can get more feedback.

 

<?php error_reporting(E_ALL) ; ini_set('display_errors','1'); ?>

 

If you're willing to help great.  If you're not, then don't say anything, cuz it's pretty annoying.

 

So is rewriting tutorials that are obviously easy to find. We'll help when you get stuck, but there's no point in duplicating content which is readily available.

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.