i'm trying to create a form that lets the visitor upload image files. I have been using the code below but keep receiving the "not set" error as if $_FILES['image'] isn't picking up the image file.
Can anyone see any errors here?
Form code:
<h1>Contact form test</h1>
<form action="php/form.php" method="post">
First name:<br>
<input type="text" name="firstname" required><br>
Last name:<br>
<input type="text" name="lastname" required><br>
<p>
<input type="file" name="image" id="image" enctype="multipart/form-data"><br>
</p>
<input type="submit" value="Submit">
</form>
php code:
// Set variables
$firstname = secure($_POST['firstname']);
$lastname = secure($_POST['lastname']);
// Form Security
function secure($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// File upload
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size = $_FILES['image']['size'];
$file_tmp = $_FILES['image']['tmp_name'];
$file_type = $_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$extensions= array("jpeg","jpg","png");
if(in_array($file_ext,$extensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be exactly 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}else{
echo "not set";
}
use enctype="multipart/form-data" in form tag
<form action="php/form.php" method="post" enctype="multipart/form-data">
First name:<br>
<input type="text" name="firstname" required><br>
Last name:<br>
<input type="text" name="lastname" required><br>
<p>
<input type="file" name="image" id="image" enctype="multipart/form-data">
<br>
</p>
<input type="submit" value="Submit">
</form>
Reference : http://php.net/manual/en/features.file-upload.post-method.php
put enctype="multipart/form-data" in form tag
Just adds a multipart attribute for form tag, which is necessary if you would like to use the form to upload files with. The enctype attribute specifies how the form-data should be encoded when submitting it to the server.
<form action="demo_post_enctype.asp" method="post" enctype="multipart/form-data">
//you code here
</form>
Also ,the enctype attribute can be used only if method="post".
Related
I'm trying to upload multiple images to server at one input(click on the button), and I have following code for index.html:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form id="contact" action="upload.php" method="post" enctype="multipart/form-data" >
<!-- image 1 -->
<label class="aa">
Upload an image
</label>
<div>
<input name="Field26" type="file" size="12" data-file-max-size="10" tabindex="1" required />
</div><br>
<!-- image 2 -->
<label class="bb">
Upload an image
</label>
<div>
<input name="Field33" type="file" size="12" data-file-max-size="10" tabindex="2" required />
</div>
</br>
<button name="submit" type="submit" value="Upload Image" data-submit="...Sending">Upload</button>
</form>
</div>
</body>
</html>
I have following upload.php code for uploading one image:
<?php
$dirCheck = 'images/$_POST[project]';
if (file_exists($dirCheck)){
echo "Sorry, directory already exists.";
$uploadOk = 0;
}else{
mkdir("images/$_POST[project]",0777,true);
echo "The new folder $_POST[project] has been created.</br>";
$uploadOk = 1;
}
$target_dir = "images/$_POST[project]/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ". ";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
How to change the code to upload multiple images? Thanks!
you have to use an array in name attribute instead:
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileupload[]" /><br><br>
<input type="file" name="fileupload[]" /><br><br>
<input type="file" name="fileupload[]" /><br><br>
<input type="submit" name="submit" value="SAVE"/>
</form>
Or you can use multiple attribute:
<input type="file" name="fileupload[]" multiple />
Then in PHP you use this:
foreach($_FILES['fileupload'] as $file) {
....
}
I have a form where i want to upload files with multiple inputs.My form looks like:
<form action="" method="post">
<input type="file" name="tax" />
<input type="file" name="ta" />
<input type="submit" name="submit" />
</form>
I do not know how to process this form..
You form doesn't work until you don't include 'enctype="multipart/form-data"', because it is necessary to use input type file.
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="tax" />
<input type="file" name="ta" />
<input type="submit" name="submit" />
</form>
Now browse the file and submit the form. You will get all file data inside $_FILES. so to check what you get inside the file data, you can use :
echo '<pre>';
print_r($_FILES)
I'm not sure whether you have gone through the tutorials before, however below is the code which will help you to process it.
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="tax" />
<input type="file" name="ta" />
<input type="submit" name="submit" />
</form>
If you upload the file, you can get the files from,
$_FILES global array, i.e. $_FILES['tax'] and $_FILES['ta'].
More info can be found on php.net
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="tax" />
<input type="file" name="ta" />
<input type="submit" name="submit" />
</form>
<?php
//print_r($_POST);
if(isset($_POST['submit'])){
$name = $_FILES['tax']['name'];
$name1 = $_FILES['ta']['name'];
$temp_name = $_FILES['tax']['tmp_name'];
$temp_name1 = $_FILES['ta']['tmp_name'];
var_dump($_FILES);
if(isset($name)){
if(!empty($name)){
var_dump($_FILES);
$location = 'images/'.$name;
if(move_uploaded_file($temp_name, $location)){
echo 'File uploaded successfully';
}
}
} else {
echo 'You should select a file to upload !!';
}
if(isset($name1)){
if(!empty($name1)){
var_dump($_FILES);
$location = 'images/'.$name1;
if(move_uploaded_file($temp_name1, $location)){
echo 'File uploaded successfully';
}
}
} else {
echo 'You should select a file to upload !!';
}
}
?>
I'm learning PHP and can not find a way to make this work. Did I write correct code to upload multiple images?
I can not think of a reason why this should be wrong.
$imageName = mysql_real_escape_string($_FILES["image, drie1, drie2, drie3, add, strip"]["name"]);
$imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
$imageType = mysql_real_escape_string($_FILES["image, drie1, drie2, drie3, add, strip"]["type"]);
HTML
<form action="file-upload.php" method="post" enctype="multipart/form-data">
Send these files:<br />
<input name="userfile[]" type="file" /><br />
<input name="userfile[]" type="file" /><br />
<input type="submit" value="Send files" />
</form>
PHP (file-upload.php)
var_dump($_FILES);
This will display the info of the uploaded files
You can add accept attribute to the input to limit the allowed filetypes by extention
Html
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="image_file[]" multiple=""> /* multiple tag is used to upload multiple files */
<input type="submit" name="Submit" value="Submit" />
</form>
Php
<?php
foreach($_FILES["image_file"]["name"] as $key => $value)
{
$name = $value;
$tmp_name = $_FILES["image_file"]["tmp_name"][$key];
$type = $_FILES["image_file"]["type"][$key];
echo $name." , ".$tmp_name." , ".$type."\n";
}
?>
I am making a Tuition Teacher finding website.I have made a sign up form. I would like to add a profile picture option to the form.
Here is the code for it,
<input type="file" name="fileToUpload" id="fileToUpload">
How do I store the image in the disk drive of the server computer?
use php move_uploaded_file
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success";
}
?>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>
</body>
</html>
move_uploaded_file(file,newloc)
file:- Specifies the file to be moved
newloc:- Specifies the new location for the file
First of all, your form should have attribute enctype="multipart/form-data". You can also use copy(file,to_file).
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
if (strlen($_FILES['fileToUpload']['tmp_name']) > 0) {
$path="path/to/new-location/IMAGENAME-With-Extension";
copy($_FILES['fileToUpload']['tmp_name'],$path);
}
I need to upload a file in a folder but the file never get in the folder. The name of the file is added correctly but no file in the folder. What is going wrong?
Php error: Undefined index: foto on line 12-16
$name= $_FILES["foto"]["name"];
$type= $_FILES["foto"]["type"];
$size= $_FILES["foto"]["size"];
$temp= $_FILES["foto"]["temp_name"];
$error= $_FILES["foto"]["error"];
if ($error > 0)
die("Error uploading file! code $error.");
else
{
if($type=="image/png" || $size > 2000000)//condition for the file
{
die("Format not allowed or file size too big!");
}
else
{
move_uploaded_file($temp,"assets/");
echo "Upload complete!";
}
}
HTML:
<form id="form" style="margin-left: 200px" action="addNewProduct.php" method="post"><br>
<br>
<br>
<div id="imageUpload">
<label for="foto">Foto</label>
<input type="file" name="foto" /><br>
</div>
<div id="infoForm">
<label for="productNaam">Productnaam</label>
<input type="text" name="productNaam"/><br>
<br>
<label for="beschrijving">Productbeschrijving</label>
<input type="text" name="productBeschrijving" /><br>
<br>
<label for="btw">BTW</label>
<input type="number" name="productBtw" /><br>
<br>
<label for="prijsinclbtw">PrijsInclBTW</label>
<input type="number" name="productPrijsInclBtw" /><br>
<br>
<br>
<br>
<input type="submit" name="submit" value="Add new Pen">
</div>
</form>
Your $_FILES["foto"]["temp_name"]; is incorrect, you should change it to $_FILES["foto"]["tmp_name"];
And your folder assets should exist in your root folder.
Your form should like this:
<form action="uploads.php" method="post" enctype="multipart/form-data">
<input type="file" name="foto">
<input type="submit" name="submit">
</form>
Please try this, hope this help you out : uploads.php
$type= $_FILES["foto"]["type"];
$size= $_FILES["foto"]["size"];
// $temp= $_FILES["foto"]["temp_name"];
$file_name = $_FILES["foto"]["name"];
$source = $_FILES['foto']['tmp_name'];
$dir = "./assets/";
$file = $dir . $file_name;
//$directory = "./assets/upload/$file_name";
if(!file_exists ($file ))
{
move_uploaded_file($source,$file );
exit();
}
else
{
echo "File exist";
}
When you upload files, form has to have enctype attribute.
<form enctype="multipart/form-data" id="form" style="margin-left: 200px" action="addNewProduct.php" method="post">
^^