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
Related
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
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>
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.