How to upload images to the disk drive of server? - php

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);
}

Related

Sending image within a form to a php server

Im trying to upload a file through a form to my php server and then display the name of the file. ATM I'm getting an error when I'm trying to submit the form:
Objekt was not found! Error 404
<html>
<body>
<form method="post" enctype="multipart/form-data" action="upload.php">
<input type="file" name="file" size="35">
<br>
<br>
<input type="submit" value="Upload" name="submit">
</body>
</html>
<?php
header('Content-type: text/plain');
if(isset($_FILES["file"])){
$file = $_FILES["file"];
echo("File: ".$file);
}
?>
if you want to upload/do any operation on same file then remove action from form. then change your code as below to echo file name
<html>
<body>
<form method="post" enctype="multipart/form-data" >
<input type="file" name="file" size="35">
<br>
<br>
<input type="submit" value="Upload" name="submit">
</body>
</html>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
if(isset($_FILES["file"])){
$file = $_FILES["file"]["name"];
echo "File: ".$file;
}
}
?>

It doesn't work to upload a file in PHP

It doesn't work to upload a file. Please help me!
Form uploadfile.html
<form action="uploadfile.php" method="post" enctype="multipart/form-data">
Select image upload
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" name="submit" value="Image Upload">
_File uploadfile.php
$upload_dir ="/uploads";
foreach ($_FILES["fileToUpload"]["error"] as $key => $error) {
if ($error ==UPLOAD_ERR_OK) {
$tmp_name=$_FILES["fileToUpload"]["tmp_name"][$key];
$name =basename($_FILES["fileToUpload"]["name"][$key]);
move_uploaded_file($tmp_name, "$upload_dir/$name");
}
}
_Don't enable upload any file.

PHP upload file web form

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".

Multi image 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";
}
?>

Upload an image and store it in two different directories

Hi, I have a form which has two file uploaders. I want to have the second file uploader upload an image and store it in two different folders.
Can anyone help me out? Here is my HTML code:
<input type="file" name="file" id="file"><br>
<input type="submit" name="upload" value="Upload Image">
<input type="file" name="thumb" id="thumb"><br>
<input type="submit" name="updthumb" value="Upload Thumb impression">
Here is my PHP code:
if(isset($_POST["upload"])){
$image ="upload/";
move_uploaded_file($_FILES["file"]["tmp_name"], $image . $_FILES["file"]["name"]);
$newfilepath2 = $image . $_FILES["file"]["name"];
mysql_query("UPDATE `candidate` SET `imgpath`='$newfilepath2' WHERE vouchno='$vouch'") or die(mysql_error());
header("refresh: 1; candproc.php?vouchno=$vouch");
}
Can anyone help in telling me how to upload the image and save it in a different folder?
Something like:
$thumbspath ="thumbs/";
$thumb = $thumbspath . $_FILES["thumb"]["name"];
move_uploaded_file($_FILES["thumb"]["tmp_name"], $thumb);
For the second file, saving into the directory thumbs
You should have set your form:
<form action="phpscript.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file"><br>
<input type="file" name="thumb" id="thumb"><br>
<input type="submit" name="upload" value="Upload files">
</form>
Note that enctype="multipart/form-data" is required for file uploads.

Categories