I have the following which uploads a single file and works fine:
<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
<input name="userfile" type="file" />
<input type="submit" value="Upload" />
</form>
<?php
$uploaddir = $campaign['upload_dir'].'/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File has been successfully uploaded.\n";
} else {
echo "Upload failed";
}
?>
When i adpat this to accept multiple files for upload, it doesnt seem to work. I dont get any errors / warnings so i am completely stumped. Here is my multiple file upload code:
<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
<input name="userfile[]" type="file" multiple />
<input type="submit" value="Upload" />
</form>
<?php
$uploaddir = $campaign['upload_dir'].'/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name'][$key]);
foreach ($_FILES["userfile"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$name = $_FILES["userfile"]["name"][$key];
move_uploaded_file( $_FILES["userfile"]["tmp_name"][$key], $uploadfile);
{
echo "File has been successfully uploaded.\n";
} else {
echo "Upload failed";
}
?>
Any suggestions on what could be wrong?
You use wrong key in $_FILES, you have to use $_FILES['userfile'], not $_FILES['files']:
<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
<input name="userfile[]" type="file" multiple />
<input type="submit" value="Upload" />
</form>
<?php
$uploaddir = $campaign['upload_dir'].'/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name'][$key]);
foreach ($_FILES["userfile"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$name = $_FILES["userfile"]["name"][$key];
move_uploaded_file( $_FILES["userfile"]["tmp_name"][$key], $uploadfile);
{
echo "File has been successfully uploaded.\n";
} else {
echo "Upload failed";
}
?>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Multiple File Ppload with PHP</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
<input type="submit" value="Upload!" />
</form>
</body>
</html>
<?php
$valid_formats = array("jpg", "png", "gif", "zip", "bmp");
$max_file_size = 1024*100; //100 kb
$path = "uploads/"; // Upload directory
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to exeicute all files
foreach ($_FILES['files']['name'] as $f => $name) {
if ($_FILES['files']['error'][$f] == 4) {
continue; // Skip file if any error found
}
if ($_FILES['files']['error'][$f] == 0) {
if ($_FILES['files']['size'][$f] > $max_file_size) {
$message[] = "$name is too large!.";
continue; // Skip large files
}
elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
continue; // Skip invalid file formats
}
else{ // No error found! Move uploaded files
if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
$count++; // Number of successfully uploaded file
}
}
}
}
?>
Related
the form upload files can not be displayed, and this is my form upload code
<?php
$imageinfo = getimagesize($_FILES['img']['tmp_name']);
if($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg') {
echo "<center><br>Sorry, we only accept GIF and JPEG images</br><br>param name: img<br>u can upload
with CSRF";
exit;
}
$uploaddir = 'ex/';
$uploadfile = $uploaddir . basename($_FILES['img']['name']);
if (move_uploaded_file($_FILES['img']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "File uploading failed.\n";
}?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" size="20" name="img" />
<input type="submit" name="upload" value="Upload" />
</form>
the result is
Sorry, we only accept GIF and JPEG images
param name: img
u can upload with CSRF
i want to display form upload files
This will work you should specify correct path
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
$imageinfo = getimagesize($_FILES['img']['tmp_name']);
if($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg') {
echo "<center><br>Sorry, we only accept GIF and JPEG images</b><br>param name: img<br>u can upload
with CSRF";
exit;
}
else{
$uploaddir = '../path-to-move/';
$uploadfile = $uploaddir . basename($_FILES['img']['name']);
if (move_uploaded_file($_FILES['img']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "File uploading failed.\n";
}
}
}?>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post" enctype="multipart/form-data">
<input type="file" size="20" name="img" />
<input type="submit" name="upload" value="Upload" />
I want to upload multiple files with predefined file name, is it possible? how to retrieve the name from php?
How to retrieve the name 'doc1' and 'doc2' from the php?
My code:
<?
$valid_formats = array("jpg", "png", "gif", "zip", "bmp");
$max_file_size = 1024*1024; //1MB
$path = "uploads/doc/"; // Upload directory
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to exeicute all files
foreach ($_FILES['files']['name'] as $f => $name) {
if ($_FILES['files']['error'][$f] == 4) {
continue; // Skip file if any error found
}
if ($_FILES['files']['error'][$f] == 0) {
if ($_FILES['files']['size'][$f] > $max_file_size) {
$message[] = "$name is too large!.";
continue; // Skip large files
}
elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
continue; // Skip invalid file formats
}
else{ // No error found! Move uploaded files
if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
$count++; // Number of successfully uploaded file
}
}
}
}
?>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Multiple File Ppload with PHP</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
Doc1: <input type="file" id="file" name="files['doc1']" multiple="multiple" accept="image/*" /><br>
Doc2: <input type="file" id="file" name="files['doc2']" multiple="multiple" accept="image/*" /><br>
<input type="submit" value="Upload!" />
</form>
</body>
</html>
Do check php code below that will return all details of files you uploaded.
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
echo "File Names:<br>Doc1= ".$_FILES['files']['name']["'doc1'"];
echo "<br>Doc2= ".$_FILES['files']['name']["'doc2'"];
echo "<br><br>";
echo "File Types:<br>Doc1= ".$_FILES['files']['type']["'doc1'"];
echo "<br>Doc2= ".$_FILES['files']['type']["'doc2'"];
echo "<br><br>";
echo "Tmp Name:<br>Doc1= ".$_FILES['files']['tmp_name']["'doc1'"];
echo "<br>Doc2= ".$_FILES['files']['tmp_name']["'doc2'"];
echo "<br><br>";
echo "Error:<br>Doc1= ".$_FILES['files']['error']["'doc1'"];
echo "<br>Doc2= ".$_FILES['files']['error']["'doc2'"];
echo "<br><br>";
echo "Size:<br>Doc1= ".$_FILES['files']['size']["'doc1'"];
echo "<br>Doc2= ".$_FILES['files']['size']["'doc2'"];
echo "<br><br>";
}
?>
Im trying to upload multiple files but my code is bypassing the "move_uploaded_file" code. What is missing?
foreach ($_FILES['file']['name'] as $file) {
$target_dir = "uploads/";
$target_file = $target_dir . $file;
if (move_uploaded_file($file, $target_file)) {
echo "The file ".$_FILES["file"]["name"]. " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
move_uploaded_file needs to get the temporary file name: $_FILES['file']['tmp_name']
foreach ($_FILES as $file) {
$target_dir = "./";
$target_file = $target_dir . $file['name'];
if (move_uploaded_file($file['tmp_name'], $target_file)) {
echo "The file " . $file["name"] . " has been uploaded.<br />";
} else {
echo "Error uploading the file " . $file["name"] . ".<br />";
}
}
Small HTML snippet for the upload form:
<form action="./testpage.php" method="post" enctype="multipart/form-data">
<input name="file1" type="file" /><br />
<input name="file2" type="file" /><br />
<input type="submit" value="Upload!" />
</form>
Try this:
for($i=0; $i < count($_FILES['file']['tmp_name']);$i++)
{
if(!is_uploaded_($_FILES['file']['tmp_name'][$i]))
{
$messages[] = 'No uploaded';
}
else
{
if(#copy($_FILES['file']['tmp_name'][$i],$target.'/'.$_FILES['file']['name'][$i]))
{
$messages[] = $_FILES['file']['name'][$i].' uploaded';
}
else
$messages[] = 'Uploading '.$_FILES['file']['name'][$i].' Failed';
}
}
}
html:
<form enctype="multipart/form-data" action="#" method="post">
<input id="uploadFile" name="file[]" type="file" />
<input id="uploadFile" name="file[]" type="file" />
<input type="submit" value="Upload" name="uploadt" />
</form>
I've trying to do an little script with which i can upload mp4-files to an webserver.
The problem is that it sometimes will upload the files and sometimes it wont.
<form enctype="multipart/form-data" action="index.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
Diese Datei hochladen: <input name="userfile" type="file" />
<input type="submit" value="Send File" name="uploadfile" />
</form>
<?php
if ( isset( $_POST['uploadfile'] ) ) {
error_reporting(-1);
$uploaddir = '/var/www/vhosts/o190.orange.fastwebserver.de/httpdocs/perl/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
$uploadfile = preg_replace('/\s+/', '_', $uploadfile);
print $uploadfile;
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File sucessfully uploaded.\n";
} else {
echo "File couldnt be uploaded!\n";
}
print "</pre>";
}
?>
I've already changed "upload_max_filesize" and "post_max_size" to 1000M.
I am trying to upload an image to server using PHP and the save inside a dir, and then returning the image url.
HTML:
<input name="photo" type="file" />
PHP
save_string_to_database( upload_img($_POST['photo']));
I have not much idea of PHP, I got a code from SO, but it did not do anything. Kindly help me to fix this code, or give a simple code to perform an upload:
function upload_img($img){
if ((($_FILES[$img]["type"] == "image/gif")
|| ($_FILES[$img]["type"] == "image/jpeg")
|| ($_FILES[$img]["type"] == "image/pjpeg")
|| ($_FILES[$img]["type"] == "image/jpg")
|| ($_FILES[$img]["type"] == "image/png"))
&& ($_FILES[$img]["size"] < 20000)
&& (strlen($_FILES[$img]["name"]) < 51)){
if ($_FILES[$img]["error"] > 0){
echo "Return Code: " . $_FILES[$img]["error"];
}
else{
// echo "Upload: " . $_FILES["image"]["name"] . "<br />";
// echo "Type: " . $_FILES["image"]["type"] . "<br />";
// echo "Size: " . ($_FILES["image"]["size"] / 1024) . " Kb<br />";
// echo "Temp file: " . $_FILES["image"]["tmp_name"] . "<br />";
if (file_exists(THEME_DIR."/images/" . $_FILES[$img]["name"])){
echo $_FILES[$img]["name"] . " already exists. ";
}
else{
move_uploaded_file($_FILES[$img]["tmp_name"],THEME_DIR."/images/" . $_FILES[$img]["name"]);
return THEME_DIR."/images/" . $_FILES[$img]["name"];
}
}
}
}
Here's a simple one.
HTML form to upload image
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
Your PHP file that does the Upload
<?php
$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo "<p>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>
Source
First you need a multipart/form-data form for uploading. This is a must :)
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
The PHP part is fairly simple:
This would result your file stored in "upload/{filename}"
The main part you want to consider is how to get the filename and back to your write_string_to_database procedure, you could do a simple script after the upload page like
save_string_to_database("upload/" . $_FILES["file"]["name"]);
would do the trick.
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
}
}
for file upload try this
<?php if(isset($_POST['submit']))
{
$ImageName = $_FILES['photo']['name'];
$fileElementName = 'photo';
$path = 'images/';
$location = $path . $_FILES['photo']['name'];
move_uploaded_file($_FILES['photo']['tmp_name'], $location);
} ?>
<form name="form1" id="form1" method="post" action="" enctype="multipart/form-data">
<input type="file" name="photo">
<input type="submit" name="submit">
</form>
This my function, variable $ten_anh is name of file image in html.
function upload_anh($ten_anh){ //$ten_anh la ten tren html vi du "avatar"
if(isset($_FILES[$ten_anh])){
$errors= array();
$file_name = $_FILES[$ten_anh]['name'];
$file_size =$_FILES[$ten_anh]['size'];
$file_tmp =$_FILES[$ten_anh]['tmp_name'];
$file_type=$_FILES[$ten_anh]['type'];
$file_ext=strtolower(end(explode('.',$_FILES[$ten_anh]['name'])));
$expensions= array("jpeg","jpg","png");
if(in_array($file_ext,$expensions)=== false){
$errors[]="Không chấp nhận định dạng ảnh có đuôi này, mời bạn chọn JPEG hoặc PNG.";
}
if($file_size > 2097152){
$errors[]='Kích cỡ file nên là 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"../images/".$file_name);
echo "Thành công!!!";
}
else{
print_r($errors);
}
}
}
Example:
- html code:
<input type="file" id="avatar" name="avatar"accept="image/png, image/jpeg" required/>
call function php: upload_anh('avatar');
<form method='post' action='' enctype='multipart/form-data'>
Name : <input type="text" name="name" required=""/><br><br>
Code : <input type="text" name="code" required=""/><br><br>
Price : <input type="text" name="price" required=""/><br><br>
Image : <input type="file" name="image" required=""/><br><br>
<button type='submit' class='buy' name="submit">Add Now</button>
</form>
<!--insert data -->
<?php
session_start();
include('db.php');
if(isset($_POST["submit"]));
{
/*echo "<pre>";
print_r($_POST);
print_r($_FILES);*/
$name = $_POST["name"];
$code = $_POST["code"];
$price = $_POST["price"];
$image = $_FILES["image"]["name"];
/* folder image save */
// $target_dir = "/var/www/html/shivam/new/upload/";
// $target_file = $target_dir.basename($_FILES["image"]["name"]);
// /*echo "1121".$target_file;*/
// $name = basename($_FILES["image"]["name"]);
// mysqli_query($con,$qry);
// /* move file */
// move_uploaded_file($_FILES['image']['tmp_name'],$target_dir.$name);
/* move_uploaded_file($tmp_name, "$target_dir/$name");*/
/* end */
$uploaddir = '/var/www/html/uploads/';
$uploadfile = $uploaddir . basename($_FILES['image']['name']);
echo '44'.$uploadfile;
echo "<p>";
if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';enter code here
print_r($_FILES);
print "</pre>";
}
?>