php flie upload error - php

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

Related

Adding a file description while uploading a file

I've created a basic upload form and everything works fine to upload, but I'm unable to find a way to add the file description to the page. I'd like it to go here under "Description":
uploads page
HTML Form:
<form action="/scripts/upload.php" method="POST" enctype="multipart/form-data">
Select a file to upload:
<input type="file" name="fileToUpload" id="fileToUpload"><br />
<textarea id="FileDescription" name="FileDescription" rows="1" placeholder="*File description" required></textarea> <br />
<input type="submit" value="Upload File" name="submit">
Script:
<?php
$target_dir = "../uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$df = disk_free_space("../uploads/");
if (isset($_POST["submit"])) {
$check = filesize($_FILES["fileToUpload"]["tmp_name"]);
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > $df) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
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.";
}
}
?>
The <textarea> tag must be linked to the form.
Use it this way:
<form action="/scripts/upload.php" id="myForm" method="POST" enctype="multipart/form-data">
Select a file to upload:
<input type="file" name="fileToUpload" id="fileToUpload"><br />
<textarea id="FileDescription" form="myForm" name="FileDescription" rows="1" placeholder="*File description" required></textarea> <br />
<input type="submit" value="Upload File" name="submit">
</form>
Only then, the content of the texarea is transferred. You can access the description then with $_POST["FileDescription"]. To use it as the Apache description, see http://httpd.apache.org/docs/2.2/mod/mod_autoindex.html#adddescription

get full file uploaded path in php

I have a question about file upload. i want to print the original filepath as it was on the users PC (eg 'C:\Documents and settings\guest\documents\test.txt')
i have try some method but only get filename.. i need to print full path
<form action="" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
// Check file size
if ($_FILES["fileToUpload"]["size"] > 5000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
echo "C:\Documents and settings\guest\documents\test.txt"; //get full path
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>

PHP File Upload upload the file even if statement is not correct

HTML:
<form action="" method="post" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="password" name="filepassword" id="filepassword">
<input type="submit" value="Upload File" name="submit">
</form>
PHP:
<?php
if(isset($_POST["submit"])) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$valueOne =trim($_POST["filepassword"]);
if($valueOne != "1212"){
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
echo "file uploaded successfully !";
}
else{
echo "file is not entered !";
}
}
?>
No matter what the code inside the if(check) statement, file get uploaded
Rewrite your code as below:-
if(!empty($_POST["submit"]) && !empty($_POST["filepassword"]) && !empty($_FILES['fileToUpload'])){
$valueOne =trim($_POST["filepassword"]);
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
if($valueOne == "1212"){ // if password is 1212 then file will upload.
// make sure you want to check == or !=
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
echo "file uploaded successfully !";
}
else{
echo "file is not entered !";
}
}
Just Swap the code within if and else
if($valueOne != "1212"){
echo "file is not entered !";
}
else{
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
echo "file uploaded successfully !";
}

PHP basic upload file script

This is my first time attempting to upload a file via PHP.
Here is my HTML:
<form role="form" action="api/upload.php" id="uploadForm" method="post" enctype="multipart/form-data">
<input id="fileToUpload" name="fileToUpload" type="file" class="file" />
<button type="submit" name="submit" id="submit">Upload</button>
</form>
Now here is the PHP script in reference "api/upload.php":
<?php
$target_dir = "files\\";
if(isset($_POST["submit"])) {
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
}
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo $uploadOk . "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else{
echo "Sorry, there was an error uploading your file.";
}
}
?>
This may be a logic error. I'm not sure. Regardless, I keep getting the message:
Sorry, there was an error uploading your file
When I echo out $uploadOk, it remains as 1. How can I find my error?
Use $_FILES["fileToUpload"]["tmp_name"] instead of $_FILES["fileToUpload"]["name"]
Should be
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
Notes:-
$_FILES["fileToUpload"]["name"] is just the name of the uploaded file. $_FILES["fileToUpload"]["tmp_name"] is the temporary file that holds the content.
Hope this helps.
[Edit 1]
I was wrong about adding a value="submit" attribute to the button. name="submit" attribute is sufficient for the isset($_POST["submit")) check.

PHP trying to move file with move_uploaded_file

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'];

Categories