My html form:
<form action='' method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Submit">
</form>
My php file:
if ($_POST['submit'] == "Submit") {
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("/downloads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"/downloads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "/downloads/" . $_FILES["file"]["name"];
}
}
there are 2 folders in my public_html: tmp and downloads, both 777 permissions (just to test)
this file is located in a .htaccess protected folder within downloads (public_html/downloads/new/update.php)
and i want the zip files to be uploaded in the downloads dir.
This code won't give me any errors, but does not upload the file. Why?
Try adding
if(move_uploaded_file(...)){
echo "it works";
} else {
echo "NOPE";
}
And
replace:
move_uploaded_file($_FILES["file"]["tmp_name"], "/downloads/" . $_FILES["file"]["name"]);
with:
move_uploaded_file($_SERVER['DOCUMENT_ROOT'].'/'.$_FILES["file"]["tmp_name"], $_SERVER['DOCUMENT_ROOT']."/downloads/" . $_FILES["file"]["name"]);
PHP needs MAX_FILE_SIZE to receive uploaded files
<form action='' method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="10240000">
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Submit">
</form>
with this form you can upload a zip archiv with 10 MB or less...
you must also set the max_post_size and upload_max_filesize in your php.ini to the same or a higher value to upload files
use this PHP code
if ((isset($_POST['submit']) && $_POST['submit'] == "Submit") AND isset($_FILES)) {
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("/downloads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
if(move_uploaded_file($_FILES["file"]["tmp_name"],
"/downloads/" . $_FILES["file"]["name"]))
echo "Stored in: " . "/downloads/" . $_FILES["file"]["name"];
else echo "file could not be processed";
}
}
This is how i solved it:
HTML:
<form enctype="multipart/form-data" method="post" action="">
<input type="hidden" name="MAX_FILE_SIZE" value="300000" />
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Submit">
</form>
PHP:
if($_POST['submit'] == "Submit") {
$filename = $_FILES["file"]["name"];
$source = $_FILES["file"]["tmp_name"];
$type = $_FILES["file"]["type"];
$name = explode(".", $filename);
$target_path = "../".$filename;
if(move_uploaded_file($source, $target_path)) {
$message = "Your .zip file was uploaded";
} else {
$message = "ERROR";
}
if($message) echo $message;
}
Related
Im new to PHP and still learning, but I want to know if I am able to do something like this. I have a basic HTML form like the one below.
<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="submit" value="Submit">
<label><input type="checkbox" id="cb" value="checkb">Make Private.</label>
</form>
And a basic upload PHP document like the one below as well.
<?php
if ($_FILES["file"]["size"] < 2097152) {
if ($_FILES["file"]["error"] > 0) {
header('Location: /');
}else {
echo "Original Name: " . $_FILES["file"]["name"] . "<br>";
echo "File Type: " . $_FILES["file"]["type"] . "<br>";
echo "File Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
$randprefix = hash('ripemd160', openssl_random_pseudo_bytes(32), false);
$filepath = "upload/" . $randprefix . $_FILES["file"]["name"];
if (file_exists($filepath)) {
echo $filepath . " already exists. ";
}else {
move_uploaded_file($_FILES["file"]["tmp_name"], $filepath);
echo "Link to file: " . '' . $filepath . '' ;
}
}
}else {
echo "File too large.";
}
?>
What i'm asking is, is it possible to have a checkbox on the HTML form that when checked the file will upload to a folder named "private" and when unchecked the file will upload to a folder named "public". Thank you.
You can use the condition like this so that than only you can get the checkbox value after the form is submitted.
if(isset($_POST['check']))
{
$filepath = "upload/private" . $randprefix . $_FILES["file"]["name"];
}
else
{
$filepath = "upload/public" . $randprefix . $_FILES["file"]["name"];
}
You have to place the name for the checkbox input type so that you can check in the upload.php page and then set the folder over there.
Replace:
<label><input type="checkbox" id="cb" value="checkb">Make Private.</label>
With:
<label><input type="checkbox" id="cb" value="checkb" name="check">Make Private.</label>
Explanation: When the user selects the checkbox at the time of submitting it will check for the check box value and it will make the $filepath as upload/private or else if the user while submitting the data he/she has not selected the check box it will change the code as per the selection if the check box value. So that you can move_upload_file() based on the selection available.
<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="submit" value="Submit">
<label><input type="checkbox" id="cb" name="checkb" value="checkb">Make Private.</label>
</form>
<?php
if ($_FILES["file"]["size"] < 2097152)
{
if ($_FILES["file"]["error"] > 0)
{
header('Location: /');
}
else
{
echo "Original Name: " . $_FILES["file"]["name"] . "<br>";
echo "File Type: " . $_FILES["file"]["type"] . "<br>";
echo "File Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
$randprefix = hash('ripemd160', openssl_random_pseudo_bytes(32), false);
if($_POST['checkb'] == "checkb")
{
$filepath = "private/" . $randprefix . $_FILES["file"]["name"];
}
else
{
$filepath = "upload/" . $randprefix . $_FILES["file"]["name"];
}
if (file_exists($filepath))
{
echo $filepath . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], $filepath);
echo "Link to file: " . '' . $filepath . '' ;
}
}
}
else
{
echo "File too large.";
}
?>
I think it is work I test It and you haven`t problems to copy!
I am trying to create a PDF file uploader, however my if statement is cutting out early and else statements are producing syntax errors. Is there way of making this IF to work?
error:
if ($_FILES['file']['error'] !== UPLOAD_ERR_OK) {
die("Upload failed with error " . $_FILES['file']['error']);
}
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $_FILES['file']['tmp_name']);
$ok = false;
switch ($mime) {
case 'image/jpeg':
case 'application/pdf':
$ok = true;
default:
die("Unknown/not permitted file type");
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("../pdf/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists: ";
echo "<p>Image URL: <strong>" . $preferences->PREF_SHOPURL . "/pdf/" . $_FILES["file"]["name"] . "</strong></p>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"../pdf/" . $_FILES["file"]["name"]);
echo "Stored in: " . "pdf/" . $_FILES["file"]["name"];
echo "<p> </p>";
echo "<p>PDF URL: <strong>" . $preferences->PREF_SHOPURL . "/pdf/" . $_FILES["file"]["name"] . "</strong></p>";
}
}
else
{
echo "Invalid file";
}
?>
The user goes to the upload page where they upload the PDF, the form then runs upload_pdf2 to upload.
Upload.php
<?php
include_once("includes/session.php");
confirm_logged_in();
//include_once("includes/functions_admin.php");
include_once("../includes/masterinclude.php");
$message = "";
$scrolltobottom = "";
$preferences = getPreferences();
//note this will also refresh the page after amending it
$pageTitle = "Site Administration: Image Upload";
include_once("includes/header_admin.php");
?>
<div class="body-indexcontent_admin">
<div class="admin">
<br/>
<h1>Image Upload</h1>
<br/>
<div class="login-box">
<form action="../_cms/upload_pdf2.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input name="image" type="file" id="file" size="50">
<p> </p>
<p> </p>
<input type="submit" name="submit" value=" -- Upload PDF -- " class="upload-button">
</form>
<?php
include_once("includes/footer_admin.php");
?>
Upload_PDF2.php
<?php
include_once("includes/session.php");
confirm_logged_in();
//include_once("includes/functions_admin.php");
include_once("../includes/masterinclude.php");
$message = "";
$scrolltobottom = "";
$preferences = getPreferences();
//note this will also refresh the page after amending it
$pageTitle = "Site Admin: Image Uploaded";
include_once("includes/header_admin.php");
?>
<div class="body-indexcontent_admin">
<div class="admin">
<br/>
<h1>PDF Uploaded</h1>
<br/>
<div class="start-page-content">
<p> </p>
<?php
$allowedExts = array("jpg", "jpeg", "gif", "png", "pdf");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ($_FILES['file']['error'] !== UPLOAD_ERR_OK) {
die("Upload failed with error " . $_FILES['file']['error']);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $_FILES['file']['tmp_name']);
$ok = false;
switch ($mime) {
case 'image/jpeg':
case 'application/pdf':
$ok = true;
default:
die("Unknown/not permitted file type");
}
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("../pdf/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists: ";
echo "<p>Image URL: <strong>" . $preferences->PREF_SHOPURL . "/pdf/" . $_FILES["file"]["name"] . "</strong></p>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"../pdf/" . $_FILES["file"]["name"]);
echo "Stored in: " . "pdf/" . $_FILES["file"]["name"];
echo "<p> </p>";
echo "<p>PDF URL: <strong>" . $preferences->PREF_SHOPURL . "/pdf/" . $_FILES["file"]["name"] . "</strong></p>";
}
}
?>
<p> </p>
</div>
<div class="start-page-content">
<p>Insert the Image URL above in the URL field on the image properties box when using the visual designer.</p>
<p> </p>
<h3>Upload another image</h3>
</div>
<?php
include_once("includes/footer_admin.php");
?>
In PHP, is it possible to upload files to the server? If so, then how can I create an upload form and insert the files to a specific page? Any extension images: .jpeg .gif or files: .txt .pdf etc. I am using XAMPP (localhost to view my website).
I have tried many tutorials, but there are lot of errors.
This is this HTML file:
<form action="fileupload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
And here is the PHP script:
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
The result is shown like this:
Upload: (file here)
Type: image/jpeg
Size: 757.521484375 Kb
Stored in: (temp file in C:)
But whenever I find my file in the tmp folder. It's nowhere to be found.
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i)
{
return "";
}
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$extension = getExtension($filename);
$extension = strtolower($extension);
if($extension=="jpg" || $extension=="jpeg" || $extension=="gif" || $extension=="txt" || $extension=="pdf" ){
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" .date("d_m_h_i_s"). $_FILES["file"]["name"]);
}
The getExtension function helps to get the extension of the file. The files will be uploaded to the upload folder.
i simply want that after i upload an image it will be redirected to a page that displays all the images. The images are saved in a folder. i have found some examples but they are so complicated, i just want to try the simple way first.
this is the HTML code for uploading image:
<form action="upload_image.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
Title:<input type="text" name="title" id="title" />
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
this is the PHP code to save the other details in the database:
include('../IFM-mobile_website/include/connect.php');
$title=$_POST['title'];
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
)
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"]) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "../IFM-mobile_website/upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
any help would be much appreciated.
Your file uploaded path and the path in which you are showing image are different
uploading to : "upload/" . $_FILES["file"]["name"]
showing it from : "../IFM-mobile_website/upload/" . $_FILES["file"]["name"]
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "../IFM-mobile_website/upload/" . $_FILES["file"]["name"];
To show all the images in a folder :
$a = glob('Your/path/*.{jpg,gif,png}',GLOB_BRACE);
print_r($a);
I used the codes(html+php script) below to upload image file
submit.html
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
php upload file upload_file.php
<?php
echo $_FILES["file"]["type"] ;
echo "<br>";
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
but it reported error:
image/jpeg
Upload: 02.jpg
Type: image/jpeg
Size: 54.2626953125 Kb
Temp file: /tmp/phpT617qx
Warning: move_uploaded_file(upload/02.jpg): failed to open stream: No such file or directory in /home/virtual/site18/fst/var/www/html/test/upload_file.php on line 29
Warning: move_uploaded_file(): Unable to move '/tmp/phpT617qx' to 'upload/02.jpg' in /home/virtual/site18/fst/var/www/html/test/upload_file.php on line 29
Stored in: upload/02.jpg
submit.html and upload_file.php are in same directory with mod 777
Welcome any comment
It could be because you should assign correct permissions for writing files into that file.
What are the permissions on upload/? Does the directory exist?