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
Related
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
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.
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 need to make an upload page in my site, I'm using an altervista trial server.
I used the tutorial http://www.w3schools.com/php/php_file_upload.asp
but the upload doesn't work. maybe, as I read is a permission issue, but I don't have the slightest idea on how to change the permissions of my folders.
Is it possible to add also pdf in the uploadable files?
thanks
uploadpage.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documento senza titolo</title>
<link href="valsilehome.css" rel="stylesheet" type="text/css">
</head>
<body>
<form action="/tmp/upload.php" 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>
</body>
</html>
upload.php
<?php
$target_dir = "/tmp/uploads";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
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.";
}
}
?>
As of w3 schools, they have created many conditions for the example. But we don't need follow it strictly. We can use it only if we needed.
Here's the Minimal Code that you can have
<!DOCTYPE html>
<html>
<body>
<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>
</body>
</html>
<?php
if (isset($_POST['submit']))
{
echo 'succesfully uploaded';
$structure = 'uploadedfiles/';
$target_file = $structure.basename($_FILES["fileToUpload"]["name"]);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
}
?>
If you use the above code you should create a folder named as uploadedfiles in the folder where you keep this file.
Else if you need to create each folder for each file upload then you should code.. It will create the file's name as folder each time.
<!DOCTYPE html>
<html>
<body>
<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>
</body>
</html>
<?php
if (isset($_POST['submit']))
{
echo 'succesfully uploaded';
$structure = $_FILES["fileToUpload"]["name"];
if (!mkdir($structure, 777, true))
{}
$target_file = $structure.basename($_FILES["fileToUpload"]["name"]);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
}
?>
As you suggested in your post, try to change the permission of the folder following the code below:
chmod("/tmp/uploads", 0777);
you are using absolute path linux type, /tmp/uploads, if you are using linux try to show permission folder with "ls -l /tmp/uploads", if you are using windows hosts maybe you can print the path $target_file, inthe w3schools example the tagtet_path doesnt have the "/" "tmp/uploads" != "/tmp/uploads".
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>