php can't move file from tmp - php

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

Related

PHP Check For File Name then Upload a File

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>";
}
}
}
}
?>

How to create a folder and upload the selected files in that folder in PHP?

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";
}
}
?>

how to find the length of the text file which user uploads

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

Some images or doc files are successfully uploaded but video is not uploaded using PHP

Some images or doc files are successfully uploaded but video is not uploaded using PHP. Can any one tell me what is the problem in my code.
<?php
include('conn.php');
?>
<html>
<head>
</head>
<body>
<form name="form1" method="POST" action="" enctype="multipart/form- data"/>
upload file<input type="file" name="f"><br/>
name:<input type="text" name="t"><br/>
<input type="submit" name="submit1" value="submit">
<?php
if(isset($_POST['submit1']))
{
$filename=$_POST['t'];
$fnm=$_FILES["f"]["name"];
$dst="./data/".$fnm;
move_uploaded_file($_FILES["f"]["tmp_name"],$dst);
$query="insert into up(filename,name,path) values('$filename','$fnm','$dst')";
$query1=mysqli_query($con,$query);
if($query1==true)
{
echo "insert data";
}
else
{
echo "not inserted";
}
}
?>
</form>
</body>
</html>

File upload not working | Php

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

Categories