I have been back forward about this, can't find a solution. My file that I am trying to upload is not moving. I'm using WAMP and my root folder is C:\wamp\www
I've checked that the directory exists in php and put in a script that if it does not exist it should be created, which works, but still it is not moving the file. What am I doing wrong?
The Form:
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
Upload.php
$target_path = "uploads/" ;
if(!file_exists($target_path)) {
mkdir($target_path, 0755, true);
echo "The directory was created";
}
$tmp_name = $_FILES['file']['tmp_name'];
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
echo $target_path;
if(move_uploaded_file($tmp_name, $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
try to change,
$tmp_name = $_FILES['file']['tmp_name'];
to
$tmp_name = $_FILES['uploadedfile']['tmp_name'];
Related
If i choose in the same folder of my target directory it said file exist but when i choose in different folder it said success but there`s no file uploaded in that folder here is my code. i get it in google
<?php
if (($_FILES['my_file']['name']!="")){
// Where the file is going to be stored
$target_dir = "/home/cidinc1802/Pictures/";
$file = $_FILES['my_file']['name'];
$path = pathinfo($file);
$filename = $path['filename'];
$ext = $path['extension'];
$temp_name = $_FILES['my_file']['tmp_name'];
$path_filename_ext = $target_dir.$filename.".".$ext;
if (file_exists($path_filename_ext)) {
echo "Sorry, file already exists.";
}else{
move_uploaded_file($temp_name,$path_filename_ext);
echo "Congratulations! File Uploaded Successfully.";
}
}
?>
<form name="form" method="post" action="fortesting.php" enctype="multipart/form-data" >
<input type="file" name="my_file" /><br /><br />
<input type="submit" name="submit" value="Upload"/>
</form>
I have an error. I made an file upload in HTML 5 and PHP. When I try to upload anything there always appears an error. Here's the code:
<form action="upload.php" method="post" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload your file" name="submit">
</form>
upload.php:
$target_path = "upload/";
$target_path = $target_path . basename($_FILES["fileToUpload"]["name"]);
if(isset($_POST["submit"])) {
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_path)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
As mentioned in the comment, the variable used in the logic test was not defined anywhere. I believe you were intending something like this.
<form action="upload.php" method="post" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload your file" name="submit">
</form>
<?php
$target_path = "upload/";
$target_file = $target_path . basename( $_FILES["fileToUpload"]["name"] );
if( isset($_POST["submit"]) ) {
if ( file_exists( $target_file ) ) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if ( move_uploaded_file( $_FILES["fileToUpload"]["tmp_name"], $target_file ) ) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"] ). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
Just change the filename in the target portion of the move_uploaded_file call
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
I'm using Azure server to open the website and linking to dropbox to store all the code files. I have an folder called 'image' but after uploading, though it shows "Uploaded!", I can't find that file.
Here's my code:
<?php
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$type = $_FILES['file']['type'];
$tmp_name = $_FILES['file']['tmp_name'];
$error = $_FILES['file']['error'];
if (isset($name)){
if(!empty($name)){
$location = 'image/';
if(move_uploaded_file($tmp_name, $location.$name)){
echo 'Uploaded!';
}else{
echo 'There was an error.';
}
}else{
echo 'Plz choose a file.';
}
}
?>
<form action="add.php" method="post" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="file" id="file">
<input type="submit" value="Upload Image" name="submit">
</form>
Your uploaded file is in the image folder that is in the same folder of your page.php.
See your code:
$location = 'image/';
if(move_uploaded_file($tmp_name, $location.$name)){
The final location of your file is $location.$name. This is image/your_file_name.your_extension
I created an upload script using the resources from tizag, the script is returning an onscreen error however no error in the apache logs.
HTML Form
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="90000000" />
Select video to upload:
Please choose a file: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
PHP Code
<?php
$target_path = "/var/www/html/upload/";
$target = $target_path . basename($_FILES['uploadedfile']['name'][0] );
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'] [0], $target_path))
{
echo "The file ". basename( $_FILES['uploadefile']['name'] [0]). " has been uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
?>
This shouldn't be very hard to impliment, but with no errors on apache it's very hard for me to troubleshoot. My php knowlegdge is limited so please bare that in mind.
Kind Regards,
Mark Couto
This line:
$target = $target_path . basename($_FILES['uploadedfile']['name'][0] );
As it stands, $target is just a stray variable and not being used anywhere else.
It should read as:
$target_path = $target_path . basename($_FILES['uploadedfile']['name'][0] );
"I created an upload script using the resources from tizag"
The Tizag tutorial you followed doesn't change their variables.
Their example:
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
Source: http://www.tizag.com/phpT/fileupload.php
Plus, you have a typo in ['uploadefile'] which should read as ['uploadedfile']
First ensure that php is configured to allow file upload.
in your "php.ini" file search for the directive, and set it ON,
file_uploads= ON
Source : http://www.w3schools.com/php/php_file_upload.asp
I have this code and would like to rename the uploaded image's name!
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="uploadedfile" accept="image/*" capture>
<input type="submit" value="Upload">
</form>
<?php
$target_path = "upload/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else {
echo "There was an error uploading the file, please try again!";
}
?>
What can i do?
When using move_uploaded_file($filename, $destination) you can specify the name of the file at the destination.
$target_path = "upload/";
$target_filename = 'filename.xyz'
$target_path = $target_path . $target_filename);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". $target_filename).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
cf. move_uploaded_file($filename, $destination)