PHP Uploading file unsuccessful - php

I'm trying to upload a file to my local server, but it keeps being unsuccessful.
All my files are inside /var/www/html/
However I made a folder called uploads in the html folder, and I changed its permissions to 777 (what I took on average from searching was the best for my needs)
this is my code:
index.html
<!DOCTYPE html>
<html>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
upload.php
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES['fileToUpload']['name']);
echo "Target File: " . $target_file . "<br />";
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!";
}
?>

Your Input file is
<input name="uploadedfile" type="file" />
so change $_FILES['fileToUpload']['name'] to $_FILES['uploadedfile']['name']
$_FILES['uploadedfile']['name'] Must have the value of Name attribute of your file field

You didn't set the variable
$target_path
you meant but not used
$target_file
instead.

Try This:
index.html
<!DOCTYPE html>
<html>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
upload.php
if(isset($_FILES["uploadedfile"]["type"]) && ($_FILES["uploadedfile"]["size"] < 5000000)){
$sourcePath = $_FILES['uploadedfile']['tmp_name'];
$file = $_FILES['uploadedfile']['name'];
$targetPath = "/uploads/".$file;
if(move_uploaded_file($sourcePath,$targetPath)){
echo "The file: ".$_FILES['uploadedfile']['name']." has been uploaded";
}else{
echo "Looks like it failed.";
}
}else{
echo "You forgot to select a file, or the file size is too large.";
}
So what this does is checks if a file exists and checks if it's smaller than 5MB. If so it moves on to the upload part.

Related

PHP Check For File Name then Upload a File

Hello and Thanks for the help!
I have Html and PHP file (2 different file):
html:
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<form enctype="multipart/form-data" action="Upload_File.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
<input name="uploadedfile" id="uploadedfile" type="file" /><br />
<br>
<input type="submit" value="Process File" Onclick="__set(document.getElementById('uploadedfile').value);" style="background-color: hsla(240, 100%, 75%,0.3);font-weight: bold;"/>
</form>
</body>
</html>
Php:
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<form enctype="multipart/form-data" action="Upload_File.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
<input name="uploadedfile" id="uploadedfile" type="file" /><br />
<br>
<input type="submit" value="Upload File" Onclick="__set(document.getElementById('uploadedfile').value);" style="background-color: hsla(240, 100%, 75%,0.3);font-weight: bold;"/>
</form>
</body>
</html>
<?php
$uploadOk = 1;
$target_path_file = "Upload_Files_All/";
$N=$_FILES['uploadedfile']['name'];
$target_path = $target_path_file . basename($N);
$size=$_FILES['uploadedfile']['size'];
$FileTypeExt = pathinfo($target_path,PATHINFO_EXTENSION);
if (isset($N))
{
if (empty($N))
{
echo "<p style='color:#ff0000;' >Please choose a file to Upload</p>";
}
else
{
if ($uploadOk==1)
{
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
echo "<strong>The file ". basename( $_FILES['uploadedfile']['name'])." is ready to upload</strong>";
}
else
{
echo "<p style='color:#ff0000;' >There was an error uploading the file, please Contact Administrator</p>";
}
}
}
}
?>
Now I want to upload files to the system
files name:
sample.xlsx
sample1.xlsx
How I modify the code that only files with the same name (sample.xlsx or sample1.xlsx) will be uploaded.
and file with different name give the user message "please change file name"
There is a not so secure method using HTML5 and input/accept.
You can use it in a Intranet Application where security is not so important.
Such Things should always be checked by the server in production.
DO NOT USE IT ON INTERNET
Only combined with a server check.
Using XLSX Mime application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
AND/OR XLS Mime, application/msexcel
<input name="uploadedfile" id="uploadedfile" type="file" accept="application/msexcel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" /><br />
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<form enctype="multipart/form-data" action="Upload_File.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
<input name="uploadedfile" id="uploadedfile" type="file" accept="application/msexcel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" /><br />
<br>
<input type="submit" value="Process File" Onclick="__set(document.getElementById('uploadedfile').value);" style="background-color: hsla(240, 100%, 75%,0.3);font-weight: bold;"/>
</form>
</body>
</html>
THE BETTER WAY
Is using the $_FILES variable
$_FILES['uploadedfile']['type'];
You can check the Mime Type
<?php
$uploadOk = 1;
$target_path_file = "Upload_Files_All/";
$accepted_mime1 = "application/msexcel";
$accepted_mime2 = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
$T=$_FILES['uploadedfile']['type'];
$N=$_FILES['uploadedfile']['name'];
if($T != $accepted_mime1 && $T != $accepted_mime2){
// We do not accept it and we unset the $_FILES
unset($_FILES);
}
$target_path = $target_path_file . basename($N);
$size=$_FILES['uploadedfile']['size'];
$FileTypeExt = pathinfo($target_path,PATHINFO_EXTENSION);
if (isset($N))
{
if (empty($N))
{
echo "<p style='color:#ff0000;' >Please choose a file to Upload</p>";
}
else
{
if ($uploadOk==1)
{
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
echo "<strong>The file ". basename( $_FILES['uploadedfile']['name'])." is ready to upload</strong>";
}
else
{
echo "<p style='color:#ff0000;' >There was an error uploading the file, please Contact Administrator</p>";
}
}
}
}
?>

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

Why file isnt uploading in the directory?

I am trying to upload files to the directory but I keep getting error or console
GET http://localhost:9999/store/uploads/.jgp
Whilst my html is
<form action="" method="POST" id="formSettingStoreLogo" enctype="multipart/form-data">
<div>
<p>Put a logo </p>
<br />
<input type="file" name="logo" />
<input type="submit" name="submitLogo" id="submitLogo" value="Upload" />
</div>
</form>
And code of php is
$target_dir = "../uploads/store/logo/";
$target_file = $target_dir . basename($_FILES["logo"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if (move_uploaded_file($_FILES["logo"]["name"], $target_file)) {
echo "\n"."The file ". basename( $_FILES["logo"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
For further information
My script for uploading images is at
C:/wamp64/www/store/settingstore.php
And the location I want to upload photos are
C:/wamp64/www/uploads/store/logo

File upload.php not working

I have following code.
index.php
<!DOCTYPE html>
<html>
<title>File Sharing</title>
<body>
<h1>File Sharing</h1><br/><br/>
<form action="upload.php" method="get" enctype="multipart/form-data">
Select File to Share:
<input type="file" name="file" id="file"/>
<button type="submit" name="submit" value="submit" id="submit" >Share</button>
</form>
<br/>
<p> or view shared files </p>
</body>
</html>
and upload.php
<?php
if(isset($_FILES['file'])){
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
// Check file size
if ($_FILES["file"]["size"] > 50000000000000000000000000) {
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["file"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["file"]["name"]). " has been shared.<br/>";
echo "<br/><p><a href='index.php'>Go Back</a>or<a href='download.php'>View Shared files</a></p>";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
else{
echo"error uploading";
}
?>
following the above my files do not get uploaded. and gives "error uploading" i got many questions for this problem i tried all but none solves my problem. What's the error???
You are using the wrong method for your form. You can't "GET" data to your server. You must "POST" it.
http://bytes.com/topic/php/insights/664241-using-html-forms-pass-data-php
Scott
My friend you should replace your GET method with POST
otherwise code working fine
<form action="upload.php" method="post" enctype="multipart/form-data">
Select File to Share:
<input type="file" name="file" id="file"/>
<button type="submit" name="submit" value="submit" id="submit" >Share</button>
</form>

php file upload failed

I followed every tutorial in the internet that I find to upload a file. But still, It failed. It gave me this error:
Warning: move_uploaded_file(/var/www/projects/upload/TASK.txt): failed to open stream: No such file or directory in /var/www/projects/test/upload.php on line 6 Warning: move_uploaded_file(): Unable to move '/tmp/phpjr2JJA' to '/var/www/projects/upload/TASK.txt' in /var/www/projects/test/upload.php on line 6 Something went wrong
index.html
<head>
<title></title>
</head>
<body>
<form method="POST" action="upload.php" enctype="multipart/form-data">
<input type="file" name="upload" ><br />
<input type="hidden" name="MAX_FILE_SIZE" value="1024" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
upload.php
<?php
$target_path = $_SERVER['DOCUMENT_ROOT'] . "/upload/";
$target_path = $target_path . basename( $_FILES['upload']['name'] );
if ( move_uploaded_file($_FILES['upload']['tmp_name'], $target_path) ) {
echo "has been uploaded";
} else {
echo "Something went wrong";
}
Can you help me and point out where I went wrong? I'm using ubuntu 12.04 and also I tried to change the permission for the /upload folder to 755 and checked the file_upload in php.ini is ON
Any help would be much appreciated. Thanks!
$_SERVER['DOCUMENT_ROOT'] gives /var/www/projects/upload.. as output
/ in the start is cause of error
Hence
try with relative path
<?php
$target_path = "upload/";
$target_path = $target_path . basename( $_FILES['upload']['name'] );
if ( move_uploaded_file($_FILES['upload']['tmp_name'], $target_path) ) {
echo "has been uploaded";
} else {
echo "Something went wrong";
}
?>
worked for me
File & Dir permissions...
Add in your FTP Program 666 to the files and dirs what you need to write.
I think ur file upload location incorrect
index.php
<html>
<head>
<title>Upload your file</title>
</head>
<body>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="Upload" value="Submit">
</form>
</body>
upload.php
<?php
$target="give the path you want to store the file/";
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
$target. $_FILES["file"]["name"]);
}
?>

Categories