I am trying to upload multiple images on my website when user clicks on upload button. I was able to do that for one image, but I am not sure how to do for multiple images.
My code:
index.php
<html>
<head></head>
<body>
<form action="backend.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file" >
<button type="submit" name="submit">button</button>
</form>
</body>
</html>
database.php
<?php
if (isset($_POST['submit'])) {
$file = $_FILES['file'];
$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['file']['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png', 'pdf');
if (in_array($fileActualExt, $allowed)) {
if($fileError === 0) {
if ($fileSize < 10000000) {
$fileNameNew = uniqid('', true).".".$fileActualExt;
$fileDestination = 'uploads/'.$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);
header("Location: index.php?uploadsuccess");
} else {
echo "Your file is too big!";
}
} else {
echo "There was an error uploading your file";
}
} else {
echo "You cannot upload files of this type!";
}
}
?>
I changed a bit in my code (index.html) so I can at least select multiple images: <input type="file" name="file[]" multiple>
but when I do that and click on upload it says:
Warning: explode() expects parameter 2 to be string, array given in /opt/lampp/htdocs/testing/backend.php on line 11
Warning: end() expects parameter 1 to be array, null given in /opt/lampp/htdocs/testing/backend.php on line 12
You cannot upload files of this type!
I assume cause it expects it to be string but I have an array now. How can I make my code work in database.php for multiple files? It works for single image, but not for multiple. If someone can reckon something?
Ok, let's say you have this HTML:
<form action="backend.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file[]" multiple />
<button type="submit" name="submit">button</button>
</form>
Normally (without multiple and with name="file"), your PHP would receive something like this:
$_FILES['file']['name'] // contains the file name
However, sending multiple files, your PHP will receive these files as following:
$_FILES['file']['name'][0] // contains the first file name
$_FILES['file']['name'][1] // contains the second file name
// ...
Read more.
Related
If i choose in the same folder of my target directory it said file exist but when i choose in different folder it said success but there`s no file uploaded in that folder here is my code. i get it in google
<?php
if (($_FILES['my_file']['name']!="")){
// Where the file is going to be stored
$target_dir = "/home/cidinc1802/Pictures/";
$file = $_FILES['my_file']['name'];
$path = pathinfo($file);
$filename = $path['filename'];
$ext = $path['extension'];
$temp_name = $_FILES['my_file']['tmp_name'];
$path_filename_ext = $target_dir.$filename.".".$ext;
if (file_exists($path_filename_ext)) {
echo "Sorry, file already exists.";
}else{
move_uploaded_file($temp_name,$path_filename_ext);
echo "Congratulations! File Uploaded Successfully.";
}
}
?>
<form name="form" method="post" action="fortesting.php" enctype="multipart/form-data" >
<input type="file" name="my_file" /><br /><br />
<input type="submit" name="submit" value="Upload"/>
</form>
hey guys so I created a website that you can upload books to and display them as a list I have a form to input the name of the book and the file type and I want them to display as the name and file type (on the same line) example (nameofabook epub) but when I try is display them it shows up like (nameofabook
new line epub) here's my code thank you
<?php
if (isset($_FILES['file']) && isset($_POST['name'])) {
$file = $_FILES['file'];
$file_name = $file['name'];
$file_type = $file['type'];
$file_tmp = $file['tmp_name'];
$file_size = $file['size'];
$file_error = $file['error'];
$file_ext = explode(".", $file_name);
$file_ext = strtolower(end($file_ext));
$allowed = array("mov", "avi", "mp4", "epub", "pdf"); //The extensions you allow
if (in_array($file_ext, $allowed)) {
if ($file_error === 0) {
if ($file_size <= 2097152) { //the maximum filesize
$file_destination = ''.$file_name; // If ' ', the file will be placed in this directory
if (move_uploaded_file($file_tmp, $file_destination)) {
echo $file_destination;
$fp = fopen('book_list.txt', "a");
fwrite($fp, $_POST['name']. "|||" .$file_destination."\n");
fwrite($fp, $_POST['type']. "|||" .$file_destination."\n");
fclose($fp);
} else {
echo "An error has been encountered while moving your file!";
}
} else {
echo "Your file is too big!";
}
} else {
echo "An error has been encountered while uploading your file!";
}
} else {
echo "You can't upload files of this type!";
}
}
?>
if anyones curious heres my html for the upload page
<!DOCTYPE html>
<html>
<head>
<title>Upload a Book</title>
<link href = "style2.css" type = "text/css" rel = "stylesheet" />
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Name: <input type="text" name="name" required/>
Type: <input type="text" name="type" required/>
File: <input type="file" name="file" required/>
<input type="submit" value="Upload" />
</form>
</body>
</html>
Firstly...
Don't upload files directly in your site! You should upload them with an API, outside, in a cloud storage. In this case, you can use Cloudinary . So, you don't need to think much about the files uploaded. Again, you can show them easily!
Don't store information like names and others(without the file) in a text file. Storing them in a MySQL database is the best way to do so.
Secondly...
Use different inputs for collecting book names and other information so that, you can display and modify them easily.
Thirdly...
Please give the code you are using for displaying data
I'm a Homo sapiens So I can write anything wrong. Please forgive me for that.
I'm trying to upload files to be more precise images from my web-pages to my webserve but it doesn't work. If i do it locally everything works fine but on the Server it doesn't work.
It doesn't give me out any Error message and i don't know why either.
I've tried to give the right permissions and it didn't work it. I tried another way to program it and it didn't work either. It always shows my first else-loop.
<?php
$SBA_ID = $_GET['SBA_ID'];
if (isset($_POST['submit'])) {
$file = $_FILES['my_file'];
print_r($file);
$fileName = $_FILES['my_file']['name'];
$fileTmpName = $_FILES['my_file']['tmp_name'];
$fileSize = $_FILES['my_file']['size'];
$fileError = $_FILES['my_file']['error'];
$fileType = $_FILES['my_file']['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png', 'pdf');
if(in_array($fileActualExt, $allowed)){
if ($fileError === 0) {
if($fileSize < 1000000){
$fileNameNew = "Auftrag".$SBA_ID.".".$fileActualExt;
$fileDestination = 'AuftragFotos/'.$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);
header("refresh:2;url= ../Startseite.php");
}else {
echo "Your File is too big!";
}
}else {
echo "There was an error uploading your file!";
}
}else {
echo "You cannot uplaod files of this Type";
}
}
My Form
<form action="<?php echo"speichern/Fotospeichern.php?SBA_ID=$SBA_ID"?>"
method="POST" enctype="multipart/form-data">
<input type="file" name="my_file"/>
<button type="submit" name="submit">UPLOAD</button>
</form>
I expect the output to be that the Image is uploaded to the "AuftragFotos" Dir but it always shows: "You cannot upload files of this Type" even though i specified that Type of file to uploaded.
You have a problem with string escaping.
Change:
<form action="<?php echo "speichern/Fotospeichern.php?SBA_ID=$SBA_ID"?>"
method="POST" enctype="multipart/form-data">
<input type="file" name="my_file"/>
<button type="submit" name="submit">UPLOAD</button>
</form>
to:
<form action="speichern/Fotospeichern.php?SBA_ID=<?php echo $SBA_ID; ?>"
method="POST" enctype="multipart/form-data">
<input type="file" name="my_file"/>
<button type="submit" name="submit">UPLOAD</button>
</form>
i am making a simple image upload event or function from simple php and html code,although i dont get any errors, i cant see my uploaded image in my folder named 'uploads',what should i do?
....html
<!DOCTYPE html>
<html>
<head>
<title>prasad</title>
</head>
<body>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit" name="submit">upload</button>
</form>
</body>
</html>
...html
...php
<?php
if (isset($_POST['submit'])){
$file = $_FILES['file'];
print_r($file);
echo "<br>";
$fileName = $_FILES['file']['name'];
$fileType=$_FILES['file']['type'];
$file_temp_name=$_FILES['file']['tmp_name'];
$file_error=$_FILES['file']['error'];
$file_size=$_FILES['file']['size'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg','jpeg','png','pdf');
if (in_array($fileActualExt, $allowed)) {
if ($file_error === 0) {
if ($file_size<1000000){
$fileNameNew = uniqid('',true).".".$fileActualExt;
$fileDestination = 'uplaods/'.$fileNameNew;
move_uploaded_file($file_temp_name, $fileDestination);
header("Location: pp.php?success");
}else {
echo "this is too big file";
}
# code...
}else{
echo "there was an error ..!";
}
}else{
echo "<h1>you couldnt choose any file..</h1>";
}
}
the only error is , i aint able to see my uploaded image
I'm trying to capture the text in a local .txt file to a PHP variable so I can save to database. I've been at it for hours and no luck. I've tried just about everything but this is what I currently have. Most of it is commented out for debugging.
As-is it just shows me a blank page and doesn't echo anything. What am I doing wrong? Any suggestions would be greatly appreciated.
EDIT:
Complete updated code. With errors on I'm getting a notice about an undefined index on line 12.
<?php
error_reporting(E_ALL); ini_set('display_errors','1');
?>
<html>
<head>
<title>upload file</title>
</head>
<body>
<?php
$size = $_FILES['file']['size'];
$filename = $_FILES['file']['tmp_name']; // name of the file
//$max_filesize = 100000; // Maximum filesize in BYTES
//$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
// if($ext = "txt") //check for .txt
// die('Only .txt files allowed.');
// if($size > $max_filesize) //check file size
// die('File is too large');
if(file_exists($filename)){
$fp = fopen($filename, "r");
$str = fread($fp, filesize($filename));
echo $str;
fclose($fp);
}
?>
</body>
</html>
And the upload form:
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
Test for $_FILES['uploadedfile']['tmp_name'] - this is the location of temporary file created on the server. $_FILES['uploadedfile']['name'] contains the original name of the uploaded file on client's side.
Update the code:
$size = $_FILES['file']['size'];
$filename = $_FILES['file']['name'];
to
$size = $_FILES['uploadedfile']['size'];
$filename = $_FILES['uploadedfile']['tmp_name'];
Documentation for the $_FILES variable can be found in PHP manual. Check out this tutorial for uploading files with PHP.
Apart from that, correct the $size($filename) to filesize($filename).
use filesize insead of $size so that it reads $str = fread($fp, filesize($filename));