Error when moving a file - php

I have an HTML web form where users can upload multiple files. I am having trouble with moving the files though.
HTML:
My HTML:
<form enctype="multipart/form-data" method="post" action="save.php">
<input type="hidden" name="MAX_FILE_SIZE" value="500000"/>
<input type="file" name="uploads[]" multiple="multiple" />
<input type="submit" name="submit" value="submit"/>
</form>
Save.php:
<?php
foreach ($_FILES['uploads']['name'] as $file) {
$target= UPLOADPATH . $file;
move_uploaded_file($file, $target)
or die('error with moving the file');
$file= time() . $_FILES['uploads']['name'];
echo $file;
}
What could I be doing wrong?

You have to use $_FILES['uploads']['tmp_name'] instead of $_FILES['uploads']['name'] for moving the file. name is the original file name but tmp_name is the current uploaded file path.

Related

Php upload image to directory not working

I know there's questions on this issue but I'm trying to get images uploaded to a directory using move_uploaded_file. I'm using the example from the php manual http://www.php.net/manual/en/function.move-uploaded-file.php but I'm getting an Undefined index: file. File is the name of my file input type so I'm not sure why it's undefined.
Here is my form:
<form action="MoveImages.php" class="dropzone" id="my-awesome-dropzone" method="post">
<input type="file" name="file" />
<input TYPE="submit" name="upload" title="Add Images" value="Add Photo"/>
</form>
Here's my PHP file:
<?php
$uploads_dir = '/Users/Jane/Desktop/NE';
$tmp_name = $_FILES["file"]["tmp_name"];
$name = $_FILES["file"]["name"];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
?>
Can anyone see where I am going wrong?
Add enctype="multipart/form-data" in your form tag.
That attribute is required when you are uploading file.

move_uploaded_file can't work in PHP

I need to move the image from temporary location to some other directory .
The input form is defined as follows:
<form action="#" method="post" id="rent_details" name="rent_details" enctype="multipart/form-data">
Upload Image :<input type="file" name="fileToUpload" id="fileToUpload">
type:- <select name="spottype" id="spottype">
<option value="xxx">xxx</option>
<option value="yyy">yyy</option>
<option value="zzz">zzz</option>
</select>
<input type="submit" id="bidm" name="bidm" value="Next"/>
</form>
I have following code for move the file ,
$target_dir = "./spotimages/";
$tmp_name = $_FILES["fileToUpload"]["tmp_name"];
$name = $_FILES["fileToUpload"]["name"];
$move_to=$target_dir.$name;
move_uploaded_file($tmp_name, $move_to);
Here,The files doesn't move spotimages folder. How to solve this. Please help me.
hi did you put this enctype="multipart/form-data" in your form tag
<form action="your_file.php" method="post" enctype="multipart/form-data">
......
......
......
</form>

Save image as session and display it

I'm trying to save a file/image as session and then display it..
here's the first page which contains the form:
<form method="post">
<input type="file" name="picture" value="upload" id="file" accept="image/*" onsubmit="return validateForm()">
</form>
Ok, so on then next I save the image as session and try to show it (which is unsuccessful)...
Code:
<?php
if (isset( $_POST['picture']))
$_SESSION['picture'] = $_POST['picture'];
echo "<img src=" $_SESSION['picture'] " border='0' /> "
?>
I do not know what you will actually do but this is the approach that best fits
when you put a file type in your form, you need to use the global variable $ _FILES
form.html
<form action="process.php" method="post" enctype="multipart/form-data">
<label for="picture">Picture:</label>
<input type="file" name="picture" id="picture"><br>
<input type="submit" name="submit" value="Upload">
</form>
process.php
<?php
session_start();
//make sure you have created the **upload** directory
$filename = $_FILES["picture"]["tmp_name"];
$destination = "upload/" . $_FILES["picture"]["name"];
move_uploaded_file($filename, $destination); //save uploaded picture in your directory
$_SESSION['picture'] = $destination;
header('Location: display_picture.php');
display_picture.php
<?php
session_start();
?>
<div>
<img src="<?php echo $_SESSION['picture']; ?>" alt="picture"/>
</div>

how do I move my uploaded file to different folder

Hi Hi trying to upload a file via a form move it to another folder and then print its name. But doesnt work don't know why.
<form method="post" action='exercice.php' id="form1">
<input type="file" name="files" id="files" onChange="submitForm();">
</form>
<?php
if (isset($_FILES['files']))
{
move_uploaded_file($_FILES['files']['tmp_name'], "uploaded/");
echo $_FILES['files']['name'];
}
?>
form missing:
enctype="multipart/form-data"
ref:
spec
<form method="post" action='exercice.php' id="form1">
<input type="file" name="files" id="files" onChange="submitForm();">
</form>
<?php
if (isset($_FILES['files']))
{
move_uploaded_file($_FILES['files']['tmp_name'], "uploaded/".$_FILES['files']['name']);
echo $_FILES['files']['name'];
}
?>
You need to include the file name in the upload path, like shown above.
Also, where is your upload (ajax) code?
Did isset($_FILES['files']) return true?

PHP---why can't I upload image with PHP?

The code
<?php
$allowed_filetypes = array('.jpg','.gif','.bmp','.png');
$max_filesize = 5242888;
$upload_path = '/files';
$filename =$_FILES['userfile']['name'];
$ext = substr($filename,strpos($filename,'.'),strlen($filename)-1); //Get the extension form the filename.
if(!in_array($ext,$allowed_filetypes))
die('the file you attempted to upload is not allowed.');
if(filesize($_FILES['userfile']['size'])>$max_filesize)
die('the file you attempted to upload is too large.');
if(!is_writable($upload_path)) {
die('you cannot upload to the specified directory,please CHMOD it to 777.');
}
if (move_uploaded_file( $_FILES['userfile']['tmp_name'],$upload_path.$filename))
{
echo 'you file upload successful.view the file here';
}
else{
echo 'failed';
}
When I upload a JPEG image, it shows the error "The file you attempted to upload is not allowed.". What's wrong with my code?
The main HTML code:
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="userfile" id="file"/>
<button>upload</button>
Please use in form tag
<form action="upload.php" method="post" enctype="multipart/form-data">
Try replace:
$ext = substr($filename,strpos($filename,'.'),strlen($filename)-1);
//get the extension form the filename
by
$ext = substr($filename,strpos($filename,'.'),4);
//get the extension form the filename
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="userfile" id="file"/>
<input type="submit" name="sendFile" value="Upload" />
</form>
that should work.
Try with a submit button, <input type="submit " name="upload" value="upload"/>.

Categories