This code working fine in localhost but working in online webserver
whenever I try to upload the files using online hosting, this code does not work for me:
<?php
if (isset($_POST['submit'])) {
$name = $_FILES['file']['name'];
$temp_name = $_FILES['file']['tmp_name'];
if (isset($name)) {
if (!empty($name)) {
$location = 'images/';
if (move_uploaded_file($temp_name, $location . $name)) {
echo 'File uploaded successfully';
}
}
} else {
echo 'You should select a file to upload !!';
}
}
?>
Here is the html code:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="save.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="submit">
</form>
</body>
</html>
What am I doing wrong?
Related
This is my first question here, I hope I can explain my problem. I have PHP code where I want to upload a PDF file and select a date, then upload it to a folder in my project called "pdfs". Then I want to download it but only until the date I chose at the upload is reached.
My upload doesn't work and I don't know why, here is my code:
<?php
include_once 'headeradmin.php';
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/styleUpload.css">
<title>Umleitung hochladen</title>
</head>
<body>
<div class="container">
<div class="row">
<form action="umleitungen.php" method="post" enctype="multipart/form-data">
<h3>Umleitungen hochladen</h3>
<input type="file" name="fileToUpload" id="fileToUpload" accept="application/pdf"><br>
<p> Datum: <input type="date" name="ablaufdatum"></p> <br>
<button type="submit" name="save">Hochladen</button>
</form>
</div>
</div>
</body>
</html>
<?php
if (isset($_POST['submit'], $_POST['ablaufdatum']) && is_uploaded_file($_FILES['fileToUpload']['tmp_name'])) {
$date = strtotime($_POST['ablaufdatum']);
if (false === $date) {
return;
}
$mimeType = mime_content_type($_FILES['fileToUpload']['tmp_name']);
$allowedFileTypes = ['application/pdf'];
if (!in_array($mimeType, $allowedFileTypes, true)) {
return;
}
$destination = 'pdfs/' . date('Y-m-d', $date) . '_' . time() . '.pdf';
if (!file_exists($destination)) {
if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $destination)) {
echo 'Supi :)';
} else {
echo 'Upload fehlgeschlagen :/';
}
}
}
include_once 'footer.php';```
Check that the web server has permissions to write to the "pdfs/" directory.
I'm very new to coding.
I'm trying to upload an image (any file for now) in a particular folder.
<?php
$name = $_FILES["file"]["name"];
$tmpName = $_FILES["file"]["tmp_name"];
if(isset($name)){
if(!empty($name)){
$location = "uploads/";
$destination_path = getcwd().DIRECTORY_SEPARATOR;
if (move_uploaded_file($tmpName,$destination_path.$location.$name)) {
echo "uploaded!";
}
else {
echo "boo";
}
}
else {
echo "please choose a file";
}
}
?>
<html>
<body>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<br>
<br>
<input type="submit" value="SUBMIT">
</form>
</body>
</html>
Everything is getting executed properly until the "move_uploaded_file" if-condition. Getting the else-echo-statement("boo")
I'm trying to upload a file in php and unable to do it. The file I'm trying to upload is a csv file, but it should not be a concern. I'm using php to upload my file. I'm also trying to process the form in the same page. Below is my code for file upload and it is not working...
<!DOCTYPE html>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="file" name="csv_file">
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])) {
if(isset($_POST['csv_file'])) {
echo "<p>".$_POST['csv_file']." => file input successfull</p>";
fileUpload();
}
}
function fileUpload () {
$target_dir = "var/import/";
$file_name = $_FILES['csv_file']['name'];
$file_tmp = $_FILES['csv_file']['tmp_name'];
if (move_uploaded_file($file_tmp, $target_dir.$file_name)) {
echo "<h1>File Upload Success</h1>";
}
else {
echo "<h1>File Upload not successfull</h1>";
}
}
?>
update your form code with enctype attribute
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype = "multipart/form-data">
<input type="file" name="csv_file">
<input type="submit" name="submit">
</form>
use enctype="multipart/form-data"
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<input type="file" name="csv_file">
<input type="submit" name="submit">
</form>
I have try below code and it's work perfect.
<!DOCTYPE html>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<input type="file" name="csv_file">
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST['submit'])) {
echo "<p>" . $_POST['csv_file'] . " => file input successfull</p>";
$target_dir = "images ";
$file_name = $_FILES['csv_file']['name'];
$file_tmp = $_FILES['csv_file']['tmp_name'];
if (move_uploaded_file($file_tmp, $target_dir . $file_name)) {
echo "<h1>File Upload Success</h1>";
} else {
echo "<h1>File Upload not successfull</h1>";
}
}
?>
</body>
</html>
It should be something like this
<!DOCTYPE html>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<input type="file" name="csv_file">
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])) {
if ($_FILES['csv_file']['size'] > 0)
{
echo "<p>".$_FILES['csv_file']['name']." => file input successfull</p>";
fileUpload();
}
}
function fileUpload () {
$target_dir = "var/import/";
$file_name = $_FILES['csv_file']['name'];
$file_tmp = $_FILES['csv_file']['tmp_name'];
if (move_uploaded_file($file_tmp, $target_dir.$file_name)) {
echo "<h1>File Upload Success</h1>";
}
else {
echo "<h1>File Upload not successfull</h1>";
}
}
?>
</body>
Below are the changes you need to make
Add enctype = "multipart/form-data" in form tag as new attribute
Change from this if(isset($_POST['csv_file'])) { to this if($_FILES['csv_file']['size'] > 0) { as you need to check size
whether its uploaded or not
Change from this echo "<p>".$_POST['csv_file']." => file input
successfull</p>"; to this echo "<p>".$_FILES['csv_file']['name']."
=> file input successfull</p>"; as you need to use $_FILES to get file name instead of $_POST
Last but not the least, complete </body> tag if you have not yet.
Upload code in PHP[without checking its extension]
<?php
if(isset($_POST['save'])){
$path="var/import/";
$name = $_FILES['csv_file']['name'];//Name of the File
$temp = $_FILES['csv_file']['tmp_name'];
if(move_uploaded_file($temp, $path . $name)){
echo "success";
}else{
echo "failed";
}
}
?>
<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="csv_file">
<input type="submit" name="save" value="submit">
</form>
First Give permission to folder where you going to upload Ex: "var/import/" folder.
<!DOCTYPE html>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype= "multipart/form-data">
<input type="file" name="csv_file">
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])) {
if(isset($_FILES['csv_file']) && $_FILES['csv_file']['size'] > 0) {
echo "<p>".$_FILES['csv_file']['name']." => file input successfull</p>";
fileUpload();
}
}
function fileUpload () {
$target_dir = "var/import/";
$file_name = $_FILES['csv_file']['name'];
$file_tmp = $_FILES['csv_file']['tmp_name'];
if (move_uploaded_file($file_tmp, $target_dir.$file_name)) {
echo "<h1>File Upload Success</h1>";
}
else {
echo "<h1>File Upload not successfull</h1>";
}
}
for uploading files, the trick is enctype="multipart/form-data"
use this form definition
I'm having issues uploading zip files and can't seem to find an answer.
Index.php
<form id="convertFile" action="convert.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="exampleInputFile">File input</label>
<input name="upload" type="file" id="inputFile">
</div>
<div class="form-group">
<button type="submit">Submit</button>
</div>
</form>
convert.php:
if(isset($_FILES)){
echo $_FILES['upload']['name'];
}else{
echo json_encode(array('status'=>'error'));
}
When I upload a zip file, I get: Notice: Undefined index: upload in C:\wamp\www\xmlconverter\convert.php on line 3
This is what chrome shows in the post header:
------WebKitFormBoundaryuFNy5dZtFj7olmD5
Content-Disposition: form-data; name="zip_file"; filename="123.zip"
Content-Type: application/x-zip-compressed
------WebKitFormBoundaryuFNy5dZtFj7olmD5--
This works on any other major file format, but can't get it to read the zip file. If I var_dump $_FILES or $_POST they are empty.
What am I missing? Why does all other files work but zip does not.
Thank you
using wamp and php 5.5.12
Where is your zip file type definition?
Anyways, let say this was the html form to upload zip files you'd have the following:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<?php if($message) echo "<p>$message</p>"; ?>
<form enctype="multipart/form-data" method="post" action="">
<label>Choose a zip file to upload: <input type="file" name="zip_file" /></label>
<br />
<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>
On the server-side script which handles the post you'd have:
<?php
if($_FILES["zip_file"]["name"]) {
$filename = $_FILES["zip_file"]["name"];
$source = $_FILES["zip_file"]["tmp_name"];
$type = $_FILES["zip_file"]["type"];
$name = explode(".", $filename);
$accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
foreach($accepted_types as $mime_type) {
if($mime_type == $type) {
$okay = true;
break;
}
}
$continue = strtolower($name[1]) == 'zip' ? true : false;
if(!$continue) {
$message = "The file you are trying to upload is not a .zip file. Please try again.";
}
$target_path = "/home/var/yoursite/httpdocs/".$filename; // change this to the correct site path
if(move_uploaded_file($source, $target_path)) {
//if you also wanted to extract the file after upload
//you can do the following
$zip = new ZipArchive();
$x = $zip->open($target_path);
if ($x === true) {
$zip->extractTo("/home/var/yoursite/httpdocs/"); // change this to the correct site path
$zip->close();
unlink($target_path);
}
$message = "Your .zip file was uploaded and unpacked.";
} else {
$message = "There was a problem with the upload. Please try again.";
}
}
?>
I used a script very similar to one posted by #unixmiah without issue on my cPanel based server. Worked great (and has for year now) but ran into issue of error "PHP, undefined index" when using locally with wamp.
Here is the mod that worked for me:
<?php
function rmdir_recursive($dir) {
foreach(scandir($dir) as $file) {
if ('.' === $file || '..' === $file) continue;
if (is_dir("$dir/$file")) rmdir_recursive("$dir/$file");
else unlink("$dir/$file");
}
rmdir($dir);
}
if(!empty($_FILES)){
//added above to script and closing } at bottom
if($_FILES["zip_file"]["name"]) {
$filename = $_FILES["zip_file"]["name"];
$source = $_FILES["zip_file"]["tmp_name"];
$type = $_FILES["zip_file"]["type"];
$name = explode(".", $filename);
$accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
foreach($accepted_types as $mime_type) {
if($mime_type == $type) {
$okay = true;
break;
}
}
$continue = strtolower($name[1]) == 'zip' ? true : false;
if(!$continue) {
$message = "<b>The file you are trying to upload is not a .zip file! Please try again...</b>";
}
$target_path = "somedir/somesubdir/".$filename; // change this to the correct site path
if(move_uploaded_file($source, $target_path)) {
$zip = new ZipArchive();
$x = $zip->open($target_path);
if ($x === true) {
$zip->extractTo("somedir/somesubdir/"); // change this to the correct site path
$zip->close();
unlink($target_path);
}
$message = "<h2>ZIP file was uploaded and content was replaced!</h2>";
} else {
$message = "There was a problem with the upload. Please try again.";
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>QikSoft ZIP Upper</title>
</head>
<body>
<?php if(!empty($message)) echo "<p>$message</p>"; ?>
<form enctype="multipart/form-data" method="post" action="">
<label><h3>Upload Your ZIP:</h3> <input type="file" name="zip_file" /></label>
<br /><br />
<input type="submit" name="submit" value="START UPLOAD" />
</form>
</body>
</html>
Also note that the echo message has been changed:
<?php if(!empty($message)) echo "<p>$message</p>"; ?>
One thing that does not work is file restriction. Currently allows other types besides ZIP. Anyone with fix, be greatly appreciated.
It's echo $_FILES['upload']['tmp_name'];
Try checking and adjusting the post_max_size value in your php.ini file, this worked for me as the default is 3M, raised the value to 128M and everything was peachy
I just made a file upload code and I was wondering why the file upload wasn't working? I checked the permission of my image folder in localhost and that seems to be ok. I don't know where the problem exactly is. Any ideas?
<?php
require "config.php";
require "functions.php";
require "database.php";
if(isset($_FILES['fupload'])){
$filename = addslashes($_FILES['fupload']['name']);
$source = $_FILES['fupload']['tmp_name'];
$target = $path_to_image_directory . $filename;
$description = addslashes($_POST['description']);
$src = $path_to_image_directory . $filename;
$tn_src = $path_to_thumbs_directory . $filename;
if (strlen($_POST['description'])<4)
$error['description'] = '<p class="alert">Please enter a description for your photo</p>';
if($filename == '' || !preg_match('/[.](jpg)|(gif)|(png)|(jpeg)$/', $filename))
$error['no_file'] = '<p class="alert">Please select an image, dummy! </p>';
if (!isset($error)){
move_uploaded_file($source, $target);
$q = "INSERT into photo(description, src, tn_src)VALUES('$description', '$src','$tn_src')";
$result = $mysqli->query($q) or die (mysqli_error($myqli));
if ($result) {
echo "Succes! Your file has been uploaded";
}
createThumbnail($filename);
}
}
?><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Upload</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<h1>My photos</h1>
<ul><?php getPhotos(); ?></ul>
<h2>Upload a photo</h2>
<form enctype="multipart/form-data" action="index.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<input type="file" name="fupload" /><br/>
<textarea name="description" id="description" cols="50" rows="6"></textarea><br/>
<input type="submit" value="Upload photo" name="submit" />
</form>
<?php
if (isset($error["description"])) {
echo $error["description"];
}
if (isset($error["no_file"])) {
echo $error["no_file"];
}
?>
</body>
</html>
Please make sure that you appended directory separator in $path_to_thumbs_directory.
And you don't need to use addslashes to $filename.