I found a nice tutorial on YouTube by Anthoniraj Amalanathan. On the video tutorial, it works fine for hem but when I try to replicate it, I get an error. Here is the code:
<form action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post">
<input type="file" name="upload[]">
<input type="file" name="upload[]">
<input type="submit" name="send" value="Send Now">
</form>
<?php
if(isset($_FILES['upload'])=== true)
{
$files = $_FILES['upload'];
for($x=0;$x<count($files['name']);$x++)
{
$name=$files['name'][$x];
$tmp_name = $file['tmp_name'][$x];
move_uploaded_file($files,'test/'.$name);
echo 'Upload OK';
}
}
?>
The message states that the error is on line 12 ($tmp_name = $file['tmp_name'][$x];) but I don't seem to figure out why.
Can some one help here?
Try this, I tested it and it works for me.
<form action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post">
<input type="file" name="upload[]">
<input type="file" name="upload[]">
<input type="submit" name="send" value="Send Now">
</form>
<?php
if(isset($_FILES['upload'])=== true) {
$files = $_FILES['upload'];
for($x=0;$x<count($files['name']);$x++) {
$name = $files['name'][$x];
$tmp_name = $file['tmp_name'][$x];
move_uploaded_file($files['tmp_name'][$x],'test/'.$name);
echo 'Upload OK';
}
}
?>
The error I got came from using an array as the temp. file location. By changing it to $files['tmp_name'][$x], it worked.
Old: move_uploaded_file($files,'test/'.$name);
New: move_uploaded_file($files['tmp_name'][$x],'test/'.$name);
Its just a typo. $file is never declared, it should be $files.
Here:
$tmp_name = $file['tmp_name'][$x];
// ^ missing s
Also here:
move_uploaded_file($files,'test/'.$name);
// ^^^^^^ shouldn't this be $tmp_name?
Try like this :
<?php
if(is_uploaded_file($_FILES['upload']['tmp_name'])){
foreach($_FILES['upload']['name'] as $x=>$name) {
$name = basename($_FILES['upload']['name'][$x});
$folder = 'test/';
$full_path = $folder.$name ;
if(move_uploaded_file($_FILES['upload']['tmp_name'][$x], $full_path)) {
echo 'Upload OK';
} else {
echo 'Upload Failed';
}
}
}else{
echo 'Upload Not Received';
}
?>
Related
I'm very new to coding.
I'm trying to upload an image (any file for now) in a particular folder.
<?php
$name = $_FILES["file"]["name"];
$tmpName = $_FILES["file"]["tmp_name"];
if(isset($name)){
if(!empty($name)){
$location = "uploads/";
$destination_path = getcwd().DIRECTORY_SEPARATOR;
if (move_uploaded_file($tmpName,$destination_path.$location.$name)) {
echo "uploaded!";
}
else {
echo "boo";
}
}
else {
echo "please choose a file";
}
}
?>
<html>
<body>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<br>
<br>
<input type="submit" value="SUBMIT">
</form>
</body>
</html>
Everything is getting executed properly until the "move_uploaded_file" if-condition. Getting the else-echo-statement("boo")
I want to send uploaded files address from html to php with array and then moved the uploaded files to images from tmp and checked their size
<form method="post" enctype="multipart/form-data" name="form1" id="form1" action="upload.php">
<p>
<input type="file" name="Img[]" />
</p>
<p>
<input type="file" name="Img[]" />
</p>
<p>
<input type="file" name="Img[]" />
</p>
<p>
<input type="submit" name="submit" id="submit" value="Submit">
</p>
</form>
<?php
$name=rand('0123456789',5).'jpg';
$files=array($_POST['Img[]']);
echo '<pre>';
print_r($files);
if($_FILES['$files[0]']['type'] == 'image/pjpeg'){
move_uploaded_file($_FILES[$files[]]['tmp_name'],'image/'.$name);
echo "success";
}
?>
I want to upload files with form
Input file elements never store in the $_POST array. You have to loop through the $_FILES array to get the file elements
if(isset($_POST['submit']))
{
for ($i=0; $i < count($_FILES['Img']['tmp_name']) ; $i++) {
$name=rand('0123456789',5);
if($_FILES['Img']['type'][$i] == 'image/jpeg')
{
move_uploaded_file($_FILES['Img']['tmp_name'][$i],'image/'.$name . '.jpg');
}
echo "success";
}
}
In this solution you can increase the number of input files with the same name as Img
Try this
<?php
if(isset($_POST['submit']))
{
foreach($_FILES['Img'] as $Img) {
$name=rand('0123456789',5);
if($Img['type'] == 'image/jpeg')
{
move_uploaded_file($Img['tmp_name'],'image/'.$name . '.jpg');
}
echo "success";
}
}
?>
This is my file uploader:
<form action="done.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit" value="upload">Upload</button>
</form>
This is what happens now:
If I do not select any file and I click on "submit", I will be redirected to the done.php.
What I need:
If there is no file selected and I click on submit, I want to stay on my site and get an error messagage "No file selected".
done.php:
if(isset($_FILES['file'])) {
$file = $_FILES['file'];
$target_file = 'files/'.basename($_FILES["file"]["name"]);
$filename = $target_file;
if (file_exists($target_file)) {
echo "file already existes";
}
else {
move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);
}
}
I updated my code now like Florian suggested:
done.php:
if(isset($_FILES['file'])) {
$file = $_FILES['file'];
$target_file = 'files/'.basename($_FILES["file"]["name"]);
$filename = $target_file;
if (file_exists($target_file)) {
echo "file already existes";
}
else {
move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);
}
}
else {
echo 'No file selected. Back to upload form';
}
What happens now is, when I do not select any file I get the error message: "file already exists". I do not understand why.
Extend your done.php as follow:
if(!file_exists($_FILES['file']['tmp_name']) || !is_uploaded_file($_FILES['file']['tmp_name'])) {
$file = $_FILES['file'];
$target_file = 'files/'.basename($_FILES["file"]["name"]);
$filename = $target_file;
move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);
} else {
echo 'No file selected. Back to upload form';
// maybe you want to include the upload form here or do something else
}
<html>
<body>
<script>
function unlock(){
document.getElementById('buttonSubmit').removeAttribute("disabled");
}
</script>
<form action="done.php" method="post" enctype="multipart/form-data">
<input type="file" onchange="unlock();" name="file">
<button type="submit" id="buttonSubmit" value="upload" disabled>Upload</button>
</form>
</body>
</html>
Depending on your "View-Code" it could be something like that
<form action="done.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="hidden" name="todo" value="fileupload"?>
<button type="submit" value="upload">Upload</button>
</form>
<?php if($_POST && !$_FILES):?>
<h3>"No file selected".</h3>
<?php endif;?>
<html>
<head>
<style>
li
{
display: inline-block;
}
</style>
<script>
function formValidation()
{
if(document.getElementById("fileName").value==""||document.getElementById("fileName").value==null)
{
alert("no file selected");
return false;
}
else
return true;
}
</script>
</head>
<body>
<form action="done.php" method="post" enctype="multipart/form-data" onSubmit="return formValidation()">
<input id="fileName" type="file" name="file">
<button type="submit" value="upload">Upload</button>
</form>
</body>
</html>
You can check it with
if(strlen($_FILES[$mcFile]['name'])==0){
echo "Error No file selected ";
}
OR
if ($_FILES["file"]["error"] > 0){
echo "Error No file selected ";
}
These both are the way to check files validation at server end
Ok I am new to mysql and php array system. I need this to work with my simple file script I have made. Below is the code like I said it works find. But I can not figure out how to add the extra inputs...
if(isset($_FILES["file"])) {
if ($_FILES["file"]["error"] > 0) {
// Error
$up = 0;
} else {
$name = $_FILES["file"]["name"];
move_uploaded_file($_FILES["file"]["tmp_name"], UPLOADS."/$name");
$up = 1;
}
header('Location: upload.php?upload='.$up);
}
if(!$steam->isLoggedin()) {
echo "Sorry Guest, You need to login with steam to upload! <br /> \n \n";
} else {
// Check our return status to see if it worked or not
if(isset($_GET['upload'])) {
if($_GET['upload'] == 1) {
echo 'Uploaded succesfully';
} else {
echo 'Upload failed';
}
}
}
<form action="upload.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<h1>Upload A ScreenShot</h1>
<em>Photo Name:</em><input name="name" type="text" id="name" />
<br />
<em>Size:</em><input name="size" type="text" id="size" />
<br />
<em>Category</em>
<select id="category" name="category">
<option>ScreenShots</option>
</select>
<br />
<em>Upload</em>:<input type="file" name="file" />
<br />
<input type="submit" name="Submit" value="Submit" />
</form>
The upload part works. But what I can not do is figure out how to add Size, Description and Category to the php code. I just do not know how to
set that part up and I do not know were to place the code. If anyone could help me I would really apricate it :).
$name = isset($_POST['name']); just returns true what you want to do with this line:
$filename = $name."_"."$type"."_".$rand."_".$_FILES['file']['name'];
and don't forget about enctype= multipart/form-data in html whithout it you could not save your file
I am trying to upload multiple files to my server. If I try to upload a single file, it works fine. but if I try more than one, it gives me an error code 4, even though it does print the name of all the files correctly. Nothing is uploaded. I have the input type set correctly. Can someone help me?
Choose Image: <input name="uploadedfile[]" type="file" multiple="true"/><br /><br /><br />
<input type="submit" value="Upload Image!" style="margin-left:100px;"/>
Below is the code:
$i=0;
foreach($_FILES['uploadedfile']['name'] as $f)
{
$file['name'] = $_FILES['uploadedfile']['name'][$i];
$file['type'] = $_FILES['uploadedfile']['type'][$i];
$file['tmp_name'] = $_FILES['uploadedfile']['tmp_name'][$i];
$file['error'] = $_FILES['uploadedfile']['error'][$i];
$file['size'] = $_FILES['uploadedfile']['size'][$i];
if ($file["error"] > 0)
{
echo "Error Code: " . $file["error"];
}
$target_path = "uploads/".basename($file["name"]);
if(move_uploaded_file($file["tmp_name"], $target_path))
{
echo basename($file['name'])."<br />";
echo basename($file['tmp_name'])."<br />";
echo $target_path;
} else{
echo "There was an error uploading the file, please try again!";
}
$i++;
}
and my HTML form
<div id="album_slider">
<div style="text-align:center;margin:20px auto;font-size:27px;">Upload Image</div>
<br style="clear:both;font-size:0;line-height:0;height:0;"/>
<div style="width:700px;margin:auto;height:250px;text-align:left;">
<form enctype="multipart/form-data" action="uploader.php" method="POST" name="form">
Image Name: <input type="text" name="image_name" id="image_name"/><br /><br /><br />
<input type="hidden" name="a_id" id="a_id" value="<?php echo $a_id; ?>"/>
Choose Image: <input name="uploadedfile[]" type="file" multiple="true"/><br /><br /><br />
<input type="submit" value="Upload Image!" style="margin-left:100px;"/>
</form>
</div>
<br style="clear:both;font-size:0;line-height:0;height:1px;"/>
</div>
Try:
foreach ($_FILES["uploadedfile"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
echo "$error_codes[$error]";
move_uploaded_file(
$_FILES["uploadedfile"]["tmp_name"][$key],
$target_path
) or die("Problems with upload");
}
}
Well its going to fail as you want to upload MULTIPLE files, but your code is only designed to handle 1 single file.
Go ahead and read through how to handle multiple files here:
http://php.net/manual/en/features.file-upload.multiple.php
Use the condition check like below code
if(isset($_FILES["qImage"]) && !empty($_FILES["qImage"]["name"])){
$imgSubQuestion = $_FILES["qImage"];
}
if(isset($_FILES["sImage"]) && !empty($_FILES["sImage"]["name"])){
$imgSolution = $_FILES["sImage"];
}