submit file through form then with same file display its contents

I’m making a single php file that takes a file uploaded by the user then it displays certain contents of the file. I’ve tried a number of approaches but keep getting messed up. What’s the best way to have a single page both be the form submitting and the form sending? The gist of what I’ve got now is:

<?php
echo '<html><body>'
if(isset($_FILE) && isset(search))
{
      fopen($_FILE['file'])
      ...
}
else
{
     <form type="POST" enctype="multipart/form-data" action="samepage.php" />
     Select file: <input type="file" name="file" /><br />
     Enter string: <input type="text" name="string" /><br />
     <input type="submit" value="send" />
}
$_FILE = $_POST = null;

I know this codes screwy but I’ve tried a number of different ways and I’m stuck!

here is a little something for you to work from, it is fully working simple script just careate a folder called uploads with a few added things in there that tell you about the file:
[php]

<? if(!empty($_POST)){ $target_path = "uploads/"; $target_path = $target_path . basename( $_FILES['file']['name']); if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['file']['name']). " has been uploaded"; $file = file_get_contents ($target_path); echo "Upload: " . $_FILES["file"]["name"] . "
"; echo "Type: " . $_FILES["file"]["type"] . "
"; echo "Size: " . floor($_FILES["file"]["size"] / 1024) . " Kb
"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; } else{ echo "There was an error uploading the file, please try again!"; } } ?> Select file:
Enter string:
[/php]

Here I extended on this a little bit now it will show an image if an image was uploaded adn it will show flash if it was uploaded, you can add the part about showing txt if it is uploaded if you want I think you can figure the rest out from this example! :wink:

Thanks man got it working! Just out of curiosity why is enctype=“multipart/form-data” needed when uploading files?

hell I will let someone else explain cause I don’t know :smiley: I would just say, because that is the way it is man!!! :smiley:

Sponsor our Newsletter | Privacy Policy | Terms of Service