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
Related
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?
I am trying to create a folder with user given name and to upload the selected files into that folder, but I am only able to create the folder and not able to move the uploaded files into that folder. Please help me.
<html>
<head>
<title>File upload</title>
</head>
<body>
<form action="#" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<br>
<label>Enter the folder name:</label>
<input type="text" name="foldername">
<br>
<input type="submit" name="submit" value="Upload">
</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
$foldername=$_POST['foldername'];
$filename=$_FILES['file']['name'];
$tmpname=$_FILES['file']['tmp_name'];
$result=mkdir($foldername);
if($result)
{
echo "created folder";
}
else
{
echo "not created folder";
}
$row=move_uploaded_file($tmpname,"$result/$filename");
if($row)
{
echo "succesffully uploaded";
}
else
{
echo "failed to upload";
}
}
?>
You have this:
$result = mkdir($foldername);
And when you try to move the files you do this:
$row = move_uploaded_file($tmpname,"$result/$filename");
$result will be a boolean based on the success or failure of mkdir. I think what you need is this:
$row = move_uploaded_file($tmpname, "$foldername/$filename");
Just made few changes to your code. mkdir() needs the foldername and permission to create. Then in the move_uploaded_file function i changed your $ result to $foldername i.e the created folder
<html>
<head>
<title>File upload</title>
</head>
<body>
<form action="#" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<br>
<label>Enter the folder name:</label>
<input type="text" name="foldername">
<br>
<input type="submit" name="submit" value="Upload">
</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
$foldername=$_POST['foldername'];
$filename=$_FILES['file']['name'];
$tmpname=$_FILES['file']['tmp_name'];
$result = mkdir($foldername,0777);
if($result)
{
echo "created folder";
}
else
{
echo "not created folder";
}
$row=move_uploaded_file($tmpname,"$foldername/$filename");
if($row)
{
echo "succesffully uploaded";
}
else
{
echo "failed to upload";
}
}
?>
Guide me to find the number of characters which are present in the text file which i upload.
This code will display the characters which are present in the .txt file`
<!DOCTYPE html>
<html>
<head>
<title>File Reader</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="60" accept=".txt" />
<input type="submit" value="Show Contents" />
</form>
<?php
if ($_FILES) {
$fileName = $_FILES['file']['tmp_name'];
$file = fopen($fileName, "r");
while (!feof($file)) {
echo fgets($file) . "";
}
while (!feof($file)) {
echo fgetc($file);
}
fclose($file);
}
?>
</body>
</html>
You could use
$_FILES['userfile']['size']
this give you the size, in bytes, of the uploaded file.
http://www.php.net/manual/en/features.file-upload.post-method.php
This is my file uploader:
<form action="done.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit" value="upload">Upload</button>
</form>
This is what happens now:
If I do not select any file and I click on "submit", I will be redirected to the done.php.
What I need:
If there is no file selected and I click on submit, I want to stay on my site and get an error messagage "No file selected".
done.php:
if(isset($_FILES['file'])) {
$file = $_FILES['file'];
$target_file = 'files/'.basename($_FILES["file"]["name"]);
$filename = $target_file;
if (file_exists($target_file)) {
echo "file already existes";
}
else {
move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);
}
}
I updated my code now like Florian suggested:
done.php:
if(isset($_FILES['file'])) {
$file = $_FILES['file'];
$target_file = 'files/'.basename($_FILES["file"]["name"]);
$filename = $target_file;
if (file_exists($target_file)) {
echo "file already existes";
}
else {
move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);
}
}
else {
echo 'No file selected. Back to upload form';
}
What happens now is, when I do not select any file I get the error message: "file already exists". I do not understand why.
Extend your done.php as follow:
if(!file_exists($_FILES['file']['tmp_name']) || !is_uploaded_file($_FILES['file']['tmp_name'])) {
$file = $_FILES['file'];
$target_file = 'files/'.basename($_FILES["file"]["name"]);
$filename = $target_file;
move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);
} else {
echo 'No file selected. Back to upload form';
// maybe you want to include the upload form here or do something else
}
<html>
<body>
<script>
function unlock(){
document.getElementById('buttonSubmit').removeAttribute("disabled");
}
</script>
<form action="done.php" method="post" enctype="multipart/form-data">
<input type="file" onchange="unlock();" name="file">
<button type="submit" id="buttonSubmit" value="upload" disabled>Upload</button>
</form>
</body>
</html>
Depending on your "View-Code" it could be something like that
<form action="done.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="hidden" name="todo" value="fileupload"?>
<button type="submit" value="upload">Upload</button>
</form>
<?php if($_POST && !$_FILES):?>
<h3>"No file selected".</h3>
<?php endif;?>
<html>
<head>
<style>
li
{
display: inline-block;
}
</style>
<script>
function formValidation()
{
if(document.getElementById("fileName").value==""||document.getElementById("fileName").value==null)
{
alert("no file selected");
return false;
}
else
return true;
}
</script>
</head>
<body>
<form action="done.php" method="post" enctype="multipart/form-data" onSubmit="return formValidation()">
<input id="fileName" type="file" name="file">
<button type="submit" value="upload">Upload</button>
</form>
</body>
</html>
You can check it with
if(strlen($_FILES[$mcFile]['name'])==0){
echo "Error No file selected ";
}
OR
if ($_FILES["file"]["error"] > 0){
echo "Error No file selected ";
}
These both are the way to check files validation at server end
I'm stuck with a simple php code, which must move a uploaded file from tmp to a desired location. Please find my code below. I'm not sure where i'm going wrong.
Any help is appreciated.
php code:
<?php
define("UPLOAD_DIR", "/srv/www/wordpress/mywebpage/uploads/");
$scr = $_FILES["jb_cv"]["tmp_name"];
$desttmp = $_FILES["jb_cv"]["name"];
$dest = UPLOAD_DIR .$desttmp;
echo " source file : ";
echo $scr."\t";
echo " destination file : ";
echo $dest;
$success = move_uploaded_file($scr,$dest);
if (!$success) {
echo "<p>Unable to save file.</p>";
exit;
}
?>
HTML code:
<html>
<head>
<title>Form</title>
</head>
<body>
<h1>Enter your name</h1>
<form method="post" action="move.php" id="contactform" name="contactform" enctype="multipart/form-data">
<label> Upload your CV</label> <input name="jb_cv" type="file" />
<input type="submit" value="SUBMIT" />
</form>
</body>
</html