I'm trying to make a PHP site that uploads files (videos in this case) and displays them. I'm trying to store them in an array and then display them on the page. The problem is, the page is only displaying the last video uploaded, i.e. the array is only storing the last uploaded file. How can I make it so that the array stores the first uploaded file in $array[0], the second in $array[1], and so on... And then display them?
Here's my code (HTML form)
<form action="upload.php" method="POST" enctype="multipart/form-data">
<p>Video to Upload: <br> <input type="file" name="video" /></p>
<p><input type="submit" name="Submit" value="Submit" /></p></form>
PHP code (upload.php)
<?php
$videoname = $_FILES["video"]["name"];
$tmp_name = $_FILES["video"]["tmp_name"];
$thevideos = array();
if (isset($videoname))
{
if (!empty($videoname))
{
$location = 'uploaded/';
move_uploaded_file($tmp_name, $location.$videoname);
$thevideos[] = $videoname; // the array is only storing $videoname in $the videos[0]??
for ($i = 0; $i <= count($thevideos); $i++)
{
echo "<video width=\"320\" height=\"240\" controls>
<source src=\"/uploaded/" .$thevideos[$i]. "\">
</video> <br>";
}
}
else
{
echo "Please choose a file.";
}
}
?>
As mentioned in the comments, if you'd like to get a listing of all files within a directory, you should consider using the scandir or glob functions.
scandir is a good selection if you need to list all content in the directory and glob is a good selection if you need to list files based on wildcards or extensions.
Related
i am working on a upload page for customers to upload some files into wordpress site. The point of doing that is get some numbers from users as variable and create the folder by using that variable. Here is my code, it actually creates a folder and puts the image inside the folder successfuly. What i want is if user inputs "5389" as number, than folder should be uploaded in "/wp-content/useruploads/5389/image.png"
here is my code
<?php /* Template Name: Create Custom Upload Path */ ?>
<?php
global $wp_filesystem;
WP_Filesystem();
$content_directory = $wp_filesystem->wp_content_dir() ;
$wp_filesystem->mkdir( $content_directory . 'useruploads' );
$target_dir_location = $content_directory . 'useruploads/';
if(isset($_POST["submittheform"]) && isset($_FILES['fileToUpload'])) {
$name_file = $_FILES['fileToUpload']['name'];
$tmp_name = $_FILES['fileToUpload']['tmp_name'];
if( move_uploaded_file( $tmp_name, $target_dir_location.$name_file ) ) {
echo "File was successfully uploaded";
} else {
echo "The file was not uploaded";
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submittheform">
</form>
i tried to have one more post method and get build number, then use it as variable but was not able to make it happen. any suggestions are appreciated
How can i count the number of uploaded files?
This is my form:
<div id="dragAndDropFiles" class="uploadArea">
<h1>Drop Images Here</h1>
</div>
<form id="sfmFiler" class="sfmform" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" multiple />
<input type="submit" name="submitHandler" id="submitHandler" class="buttonUpload" value="Upload">
</form>
and this is the piece of php which uploads the files:
if($_SERVER['REQUEST_METHOD'] == "POST") {
$tmpFilePath = $_FILES['file']['tmp_name'];
$newFilePath = $dir.'/' . $_FILES['file']['name'];
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
echo "xxx files are successfully uploaded";
}
}
In this code you are getting only one file thats why you are getting count result 1. if change your input file name like "file[]"
<input type="file" name="file[]" id="file" multiple />
and then use the below line code you will get your desire result. Cause its needs an array filed to hold the input data.
<?php echo count($_FILES['file']['name']); ?>
Thanks, i tried in my system get the result.
AFriend is correct. The above answers always return 1.
Try:
echo count(array_filter($_FILES['file']['name']))
Worked for me anyway.
_t
Using the array_filter function it works
try
$countfiles = count(array_filter($_FILES['file']['name']));
It returns 0 in case of NULL, 1 in case of 1 file uploaded, etc.
Check this answer
<?php echo count($_FILES['file']['name']); ?>
php multiple file uploads get the exact count of files a user uploaded and not the count of all input fields in the array
You could use the count function:
$no_files = count($_FILES);
If no files are selected and your file count is 1, you can use this line before moving the file:
if (!empty($_FILES['file']['tmp_name'][0])) {
for($i=0;$i<$countfiles;$i++){
How can i count the number of uploaded files?
This is my form:
<div id="dragAndDropFiles" class="uploadArea">
<h1>Drop Images Here</h1>
</div>
<form id="sfmFiler" class="sfmform" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" multiple />
<input type="submit" name="submitHandler" id="submitHandler" class="buttonUpload" value="Upload">
</form>
and this is the piece of php which uploads the files:
if($_SERVER['REQUEST_METHOD'] == "POST") {
$tmpFilePath = $_FILES['file']['tmp_name'];
$newFilePath = $dir.'/' . $_FILES['file']['name'];
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
echo "xxx files are successfully uploaded";
}
}
In this code you are getting only one file thats why you are getting count result 1. if change your input file name like "file[]"
<input type="file" name="file[]" id="file" multiple />
and then use the below line code you will get your desire result. Cause its needs an array filed to hold the input data.
<?php echo count($_FILES['file']['name']); ?>
Thanks, i tried in my system get the result.
AFriend is correct. The above answers always return 1.
Try:
echo count(array_filter($_FILES['file']['name']))
Worked for me anyway.
_t
Using the array_filter function it works
try
$countfiles = count(array_filter($_FILES['file']['name']));
It returns 0 in case of NULL, 1 in case of 1 file uploaded, etc.
Check this answer
<?php echo count($_FILES['file']['name']); ?>
php multiple file uploads get the exact count of files a user uploaded and not the count of all input fields in the array
You could use the count function:
$no_files = count($_FILES);
If no files are selected and your file count is 1, you can use this line before moving the file:
if (!empty($_FILES['file']['tmp_name'][0])) {
for($i=0;$i<$countfiles;$i++){
I am trying to build a web page where users can edit images portrayed on their public pages. There are 3 images that display on their public page and I've set up 3 HTML forms in order to handle the 3 separate files.
I only have 1 listed below because once I figure out the fix to 1 I'll be able to duplicate the fix to the other 2,
I have other upload pages on my website and they work fine, but for some reason this page is giving me trouble. I can select a file but when I want to upload it my php code doesn't read that there is anything being posted.
*I've commented out the function call (I know it works) I just need to know why my php code won't read that there is a file there.
If I had to guess it'd be something with how it's named or how it's being tossed to the php code.
The code looks like this:
<div class="academic" style="width:250px;">
<br>
<?php
if(isset($_FILES['aca']) === true)
{
echo 'please print'; //It doesn't
if(empty($_FILES['aca']['name']) === true)
{
echo 'Please choose a file! <br>';
}
else
{
$allowed = array('jpg', 'jpeg', 'gif', 'png');
$file_name = $_FILES['aca']['name'];
$file_extn = strtolower(end(explode('.', $file_name)));
$file_temp = $_FILES['aca']['tmp_name'];
echo '<br>';
echo '<br>';
print_r($_FILES['aca']['tmp_name']);
echo '<br>';
echo '<br>';
if(in_array($file_extn, $allowed) === true) {
//upload_image($file_temp,$file_extn);
echo 'You have uploaded a picture!';
echo '<b><h3>Press submit again to finish the upload</h3></b>';
//echo "<script>window.top.location='../hidden/viewPNM.php?id=$permi'</script>";
}
else
{
echo 'Incorrect File Type!! <br> Allowed: ';
echo implode(', ', $allowed);
}
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="aca" id="aca">
<input type="submit" value="Upload">
</form>
</div>
</span>
Okay...here is what you can try to do:
Move the whole PHP code UNDER the form code. I remember that sometimes wierd bugs with that were happening, and the code didnt run
If that doesnt work, do following:
Change <input type="file" name="aca" id="aca"> to <input multiple="true" type="file" name="aca[]" />
Add this code above the submit button: <input type="hidden" name="sendfiles" value="Send Files" />
Replace if(isset($_FILES['aca']) === true) with if(isset($_POST['sendfiles']))
If this doesn't work, I can offer you a way to allow multiple files being uploaded from one button. (Took the code from my website I made before)
I want to allow a user to upload multiple text files using a simple HTML form, and then save them to a string. After that, I want to display them to the user. For example, say I have three text files with the following contents:
file1.txt: this is some text.
file2.txt: this is some more text.
file3.txt: even more text!
When the user uploads them using an HTML form, I want to save them to a string and display them like so:
this is some text.
this is some more text.
even more text!
I have the following (incomplete) code that attempts to get only one file:
Upload text documents: <br /><br>
<form action="output.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
<?php
$doc = $_POST['file'];
echo $doc;
?>
How can I loop through the files and save them to a string, then display them in the best way possible?
$filenames = array();
if(isset($_FILES["documents"]))
{
for($i=0; $_FILES['documents']['name'][$i] != '' ; $i++)
{
$filenames[$i] = $_FILES['documents']['name'][$i];
$myfile = fopen("$_FILES['documents']['name'][$i]", "r") or die("Unable to open file!");
echo fread($myfile,filesize("$_FILES['documents']['name'][$i]"));
fclose($myfile);
}
}
I think this is helps for you!!!!!!!1
In completion of White Marcus answer
use the multiple="multiple"attribute in the input file element.
You should have : $_FILES variable, instead of $_POST
<?php
var_dump($_FILES);
?>
To print a file use file_put_contents()
Best regards
$filenames = array();
if(isset($_FILES["documents"]))
{
for($i=0; $_FILES['documents']['name'][$i] != '' ; $i++)
{
$filenames[$i] = $_FILES['documents']['name'][$i];
}
print_r($filename);
}
use the multiple="multiple"attribute in the input file element.