Hello and Thanks for the help!
I have Html and PHP file (2 different file):
html:
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<form enctype="multipart/form-data" action="Upload_File.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
<input name="uploadedfile" id="uploadedfile" type="file" /><br />
<br>
<input type="submit" value="Process File" Onclick="__set(document.getElementById('uploadedfile').value);" style="background-color: hsla(240, 100%, 75%,0.3);font-weight: bold;"/>
</form>
</body>
</html>
Php:
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<form enctype="multipart/form-data" action="Upload_File.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
<input name="uploadedfile" id="uploadedfile" type="file" /><br />
<br>
<input type="submit" value="Upload File" Onclick="__set(document.getElementById('uploadedfile').value);" style="background-color: hsla(240, 100%, 75%,0.3);font-weight: bold;"/>
</form>
</body>
</html>
<?php
$uploadOk = 1;
$target_path_file = "Upload_Files_All/";
$N=$_FILES['uploadedfile']['name'];
$target_path = $target_path_file . basename($N);
$size=$_FILES['uploadedfile']['size'];
$FileTypeExt = pathinfo($target_path,PATHINFO_EXTENSION);
if (isset($N))
{
if (empty($N))
{
echo "<p style='color:#ff0000;' >Please choose a file to Upload</p>";
}
else
{
if ($uploadOk==1)
{
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
echo "<strong>The file ". basename( $_FILES['uploadedfile']['name'])." is ready to upload</strong>";
}
else
{
echo "<p style='color:#ff0000;' >There was an error uploading the file, please Contact Administrator</p>";
}
}
}
}
?>
Now I want to upload files to the system
files name:
sample.xlsx
sample1.xlsx
How I modify the code that only files with the same name (sample.xlsx or sample1.xlsx) will be uploaded.
and file with different name give the user message "please change file name"
There is a not so secure method using HTML5 and input/accept.
You can use it in a Intranet Application where security is not so important.
Such Things should always be checked by the server in production.
DO NOT USE IT ON INTERNET
Only combined with a server check.
Using XLSX Mime application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
AND/OR XLS Mime, application/msexcel
<input name="uploadedfile" id="uploadedfile" type="file" accept="application/msexcel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" /><br />
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<form enctype="multipart/form-data" action="Upload_File.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
<input name="uploadedfile" id="uploadedfile" type="file" accept="application/msexcel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" /><br />
<br>
<input type="submit" value="Process File" Onclick="__set(document.getElementById('uploadedfile').value);" style="background-color: hsla(240, 100%, 75%,0.3);font-weight: bold;"/>
</form>
</body>
</html>
THE BETTER WAY
Is using the $_FILES variable
$_FILES['uploadedfile']['type'];
You can check the Mime Type
<?php
$uploadOk = 1;
$target_path_file = "Upload_Files_All/";
$accepted_mime1 = "application/msexcel";
$accepted_mime2 = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
$T=$_FILES['uploadedfile']['type'];
$N=$_FILES['uploadedfile']['name'];
if($T != $accepted_mime1 && $T != $accepted_mime2){
// We do not accept it and we unset the $_FILES
unset($_FILES);
}
$target_path = $target_path_file . basename($N);
$size=$_FILES['uploadedfile']['size'];
$FileTypeExt = pathinfo($target_path,PATHINFO_EXTENSION);
if (isset($N))
{
if (empty($N))
{
echo "<p style='color:#ff0000;' >Please choose a file to Upload</p>";
}
else
{
if ($uploadOk==1)
{
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
echo "<strong>The file ". basename( $_FILES['uploadedfile']['name'])." is ready to upload</strong>";
}
else
{
echo "<p style='color:#ff0000;' >There was an error uploading the file, please Contact Administrator</p>";
}
}
}
}
?>
Related
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";
}
}
?>
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 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.
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
I am trying to upload multiple images to a folder using PHP using this tutorial I managed:
In the PHP form
<?php
$success = 0;
$fail = 0;
$uploaddir = 'uploads/';
for ($i=0;$i<4;$i++)
{
if($_FILES['userfile']['name'][$i])
{
$uploadfile = $uploaddir . basename($_FILES['userfile']['name'][$i]);
$ext = strtolower(substr($uploadfile,strlen($uploadfile)-3,3));
if (preg_match("/(jpg|gif|png|bmp)/",$ext))
{
if (move_uploaded_file($_FILES['userfile']['tmp_name'][$i], $uploadfile))
{
$success++;
}
else
{
echo "Error Uploading the file. Retry after sometime.\n";
$fail++;
}
}
else
{
$fail++;
}
}
}
echo "<br> Number of files Uploaded:".$success;
echo "<br> Number of files Failed:".$fail;
?>
In the HTML form
<form enctype="multipart/form-data" action="upload.php" method="post">
Image1: <input name="userfile[]" type="file" /><br />
Image2: <input name="userfile[]" type="file" /><br />
Image3: <input name="userfile[]" type="file" /><br />
Image4: <input name="userfile[]" type="file" /><br />
<input type="submit" value="Upload" />
</form>
As you can see in the HTML form the input name is userfile[] for all of them. Now in my HTML for the input names are as follows: picture01, picture02, picture 03, etc...
How can I modify the PHP code to have my input names {: picture01, picture02, picture 03} rather than userfile[].
Thanks.
UPDATE
I want the above to fit in my HTML Form
<form enctype="multipart/form-data" action="upload.php" method="post">
Picture 01<input id="picture01" name="picture01" type="file" ><br />
Picture 02<input id="picture02" name="picture02" type="file" ><br />
Picture 03<input id="picture03" name="picture03" type="file" ><br />
Picture 04<input id="picture04" name="picture04" type="file" ><br />
<input type="submit" value="Upload" />
</form>
This code is working locally. It uses a combination of your code and the example from php.net. You should probably use pathinfo to get the extension but that's a minor detail.
form.html
<form enctype="multipart/form-data" action="upload.php" method="post">
Image1: <input name="userfile[]" type="file" /><br />
Image2: <input name="userfile[]" type="file" /><br />
Image3: <input name="userfile[]" type="file" /><br />
Image4: <input name="userfile[]" type="file" /><br />
<input type="submit" value="Upload" />
</form>
upload.php:
<?php
error_reporting(E_ALL);
ini_set("display_errors",1);
$success = 0;
$fail = 0;
$uploads_dir = 'uploads';
$count = 1;
foreach ($_FILES["userfile"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["userfile"]["tmp_name"][$key];
$name = $_FILES["userfile"]["name"][$key];
$uploadfile = "$uploads_dir/$name";
$ext = strtolower(substr($uploadfile,strlen($uploadfile)-3,3));
if (preg_match("/(jpg|gif|png|bmp)/",$ext)){
$newfile = "$uploads_dir/picture".str_pad($count++,2,'0',STR_PAD_LEFT).".".$ext;
if(move_uploaded_file($tmp_name, $newfile)){
$success++;
}else{
echo "Couldn't move file: Error Uploading the file. Retry after sometime.\n";
$fail++;
}
}else{
echo "Invalid Extension.\n";
$fail++;
}
}
}
echo "<br> Number of files Uploaded:".$success;
echo "<br> Number of files Failed:".$fail;
When you have change the names in the form, change the name when you try to get an array element of the file
ex.
echo $_FILES["picture$i"]['name'];
and change the for as this
for ($i=1;$i<=4;$i++)