I'm trying to upload a file to this directory:
with this code:
<?php
$target_dir = "../../assets/image/product/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$image = $_POST['fileToUpload'];
$add = $_POST['add'];
$merk = $_POST['merk'];
$category = $_POST['category'];
$color = $_POST['color'];
$size = $_POST['size'];
$price = $_POST['price'];
$stock = $_POST['stock'];
if(isset($add)){
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
mysql_query("INSERT INTO item VALUES('','$merk','$category','$color','$size','$price','$stock','$image')");
echo "<script>window.location='item.php';</script>";
}
?>
but the uploaded file is not appearing in target directory
Make sure you have add enctype="multipart/form-data" to your <form> element in your html file
Basically you have to find what error messages for.
add this line at the top of your code
error_reporting(E_ALL);
and this one, if it's your local (not live) server
ini_set('display_errors',1);
so, you will be able to see the errors on screen
Finally, for the files upload make sure that upload folder have full permission and you have to check $_FILES['fileToUpload']['error']) first. it it's not 0, refer to the manual page for the message.
Related
I'm trying to create a file upload only my current script doesn't seem to work as I believe it should.
I've managed to get the data saved in the MySQL table okay but I can't seem to get the file into the 'uploads' directory?
if(isset($_POST['new']) && $_POST['new']==1){
$folder = "uploads/";
$upload_image = $folder . basename($_FILES["image1"]["name"]);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $upload_image);
$trn_date = date("Y-m-d H:i:s");
$brand =$_REQUEST['brand'];
$user =$_SESSION["username"];
$model = $_REQUEST['model'];
$serial =$_REQUEST['serial'];
$purchasedate = $_REQUEST['purchasedate'];
$img1 =$_REQUEST['image1'];
$ins_query="insert into table
(`user`,`trn_date`,`brand`,`model`,`serial`,`purchasedate`,`image1`)values
('$user','$trn_date','$brand','$model','$serial','$purchasedate','$img1')";
mysqli_query($con,$ins_query)
or die(mysql_error());
$status = "added successfully.
</br></br><a href='home.php'>home</a>";
}
$upload_image = $folder . basename($_FILES["image1"]["name"]);
move_uploaded_file($_FILES["image1"]["tmp_name"], $upload_image);
What if trying directly what the doc said ;)
$uploads_dir = '/uploads';//try over 'uploads' too or create a 'uploads' folder in you app_root
foreach ($_FILES["image1"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["image1"]["tmp_name"][$key];
$name = $_FILES["image1"]["name"][$key];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
}
Possible Link
I have found an issue in your code.
$folder = "uploads/";
$upload_image = $folder . basename($_FILES["image1"]["name"]);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $upload_image);
In this case the file does not have the extension
$folder = "uploads/";
$ext = explode('.', basename( $_FILES['image1']['name']));
$upload_image = $folder . basename($_FILES["image1"]["name"].$ext);
move_uploaded_file($_FILES["image1"]["tmp_name"], $upload_image);
The uploads folder is in the same directory that is the php script?
You also can verify if the file was correctly moved doing the following test
if (file_exists($upload_image)){
echo "true";
}
Provide a file upload validation before uploading into server it will prevent server from malicious files
How to give file validation in php : Link
I want to upload excel file using to php and wanted to perform some file operation in that excel file. I am not able to upload the file , If any one have some Idea please help , in the server side how to get the path from where the file has been uploaded?
$target_dir = 'uploads/'; is not working for me. and My file is in D: Please help.
PHP CODE:
<?php
if(isset($_POST['SubmitButton'])){ //check if form was submitted
$target_dir = 'uploads/';
$target_file = $target_dir . basename($_FILES["filepath"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
require_once dirname(__FILE__) . '/Includes/Classes/PHPExcel/IOFactory.php';
$inputFileType = PHPExcel_IOFactory::identify($target_file);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($target_file);
$i=2;
$val=array();
$count=0;
for($i=2;$i<34;$i++)
{
$val[$count++]=$objPHPExcel->getActiveSheet()->getCell('C'.$i)->getValue();
}
//echo'<pre>';print_r($val);
}
?>
HTML CODE:
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="filepath" id="filepath"/></td><td><input type="submit" name="SubmitButton"/>
</body>
You first need to upload the file before the read line:
$target_dir = 'uploads/';
$target_file = $target_dir . basename($_FILES["filepath"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
move_uploaded_file($_FILES["filepath"]["tmp_name"], $target_file);
// rest of your code...
now you should be able to continue working on the file.
before this, the file never have been in your uploads folder.
if(isset($_POST['SubmitButton'])){
try { //attached file formate
$statement = $db->prepare("SHOW TABLE STATUS LIKE 'table_name'");
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row)
$new_id = $row[10]; //10 fixed
$up_filename=$_FILES["filepath"]["name"];
$file_basename = substr($up_filename, 0, strripos($up_filename, '.')); // strip extention
$file_ext = substr($up_filename, strripos($up_filename, '.')); // strip name
$f2 = $new_id . $file_ext;
move_uploaded_file($_FILES["filepath"]["tmp_name"],"uploads/" . $f2);
// Client's info Insert MySQl
$statement = $db->prepare("INSERT INTO table_name (files) VALUES (?)");
$statement->execute(array($f2));
$success_message = "Excel file upload successfully!";
}
catch(Exception $e) {
$error_message = $e->getMessage();
}
}
Form code:
<form action="" method="post" enctype="multipart/form-data"> <input
type="file" name="filepath" id="filepath"/></td><td><input
type="submit" name="SubmitButton"/>
</form>
U did not write code to upload file.
move_uploaded_file()
You have to move your file from the temporary upload directory to the directory you want it in with the move_uploaded_file() function.
There are 2 arguments you need to put for the move_uploaded_file() function - the temporary file you just uploaded (ex. $_FILES["file"]["tmp_name"]), and then the directory you want to move it to... So it would end up looking something like this: move_uploaded_file($_FILES["file"]["tmp_name"], $path_of_new_file)
I want to upload files from my server. I had written this piece of code months ago when it worked fine, but now I have no idea what's going wrong.
I basically want to move the file to a folder "uploads" in my server and then store the path in the database.
The uploaded file doesn't get reflected in the database.
if(isset($_POST['submit']))
{
require("dbconn.php");
$filename = $_POST['filename'];
$name = $filename . "." . pathinfo($_FILES['ufile']['name'],PATHINFO_EXTENSION);
//$name = $_FILES['ufile']['name'];
//$size = $_FILES['file']['size']
//$type = $_FILES['file']['type']
$tmp_name = $_FILES['ufile']['name']; //was tmp_name
$error = $_FILES['ufile']['error'];
if(isset($name))
{
if(!empty($name))
{
$location = 'uploads/';
if(move_uploaded_file($tmp_name, $location.$name))
{
echo "hi";
$filename = $_POST['filename'];
$filepath = $location.$name;
$advname = $_POST['advname'];
$year = $_POST['year'];
$cname = $_POST['cname'];
$ctype = $_POST['ctype'];
$sqlq = "INSERT INTO file(filename, filepath, advname, year, cname, ctype) VALUES ('".$filename."','".$filepath."','".$advname."','".$year."','".$cname."','".$ctype."');";
$result = mysql_query($sqlq);
if(!$result)
{
die("Error in connecting to database!");
}
}
}
}
}
There seems to be a problem in the
if(move_uploaded_file($tmp_name, $location.$name))
statement. This condition is evaluated as false.
If $_FILES['ufile']['error'] gives you a value of 1 then the upload_max_filesize in your config is set to a smaller value that the file you are trying to upload.
You cannot change the upload_max_filesize from within your code.
If you have access to your php.ini, .htaccess, httpd.conf or .user.ini you can change this value in any of those files.
So you would need to add or modify the parameter from its default of 2M (2meg)
upload_max_filesize = 20M
Its also a good idea to check the post_max_size at the same time as this may also have an effect on the max size of the file uploaded, make sure post_max_size is larger than upload_max_filesize as this should allow extra space for the other fields that you will be uploading as part of the post
I am new to php. i want to upload images from html form into file. i searched on net but unable to find anything helpful. Please tell me how to uplaod images in file rather than database. i know the method of uploading images in database, and its very easy. Is there any way like this of uploading images in file. sorry for my bad english.
$_FILES['image']['tmp_name'];
$original_image=file_get_contents ($_FILES['image']['tmp_name']);
$name= $_FILES['image']['name'];
Take a look at move_uploaded_file.
This is an example from the link:
<?php
$uploads_dir = '/uploads';
foreach ($_FILES["image"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["image"]["tmp_name"][$key];
$name = $_FILES["image"]["name"][$key];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
}
?>
Shorter example without support for multiple files/error checking:
<?php
$tmp_name = $_FILES["image"]["tmp_name"];
$name = $_FILES["image"]["name"];
move_uploaded_file($tmp_name, "uploads/$name");
?>
Even shorter as a one-liner:
<?php
move_uploaded_file($_FILES["image"]["tmp_name"], "uploads/" . $_FILES["image"]["name"]);
?>
do you mean you want to store your image to your server.
just use these two functions
<?php
copy($_FILES['image']['tmp_name'], $imgPath); //copy your image to a specific path on server
move_uploaded_file($_FILES['image']['tmp_name'], $imgPath); //copy your image to a specific path on server and delete uploaded image in temp folder
?>
I'm trying to move uploaded files into a folder, but i can't do it. Somebody see the error?
My html code has this line:
<form id="form" method="post" action="save_new_article.php" enctype="multipart/form-data">
And this is php code:
include("bd.php");
header('Content-type: text/html; charset=windows-1251');
$category = $_POST['category'];
$name = $_POST['name'];
$description = $_POST['full_description'];
$price = $_POST['price'];
$way = "../galery";
$file = $_FILES['photo']['tmp_name'];
$file_name = $_FILES['photo']['name'];
$file_name = ext($file_name);
$way .= "/".$file_name;
move_uploaded_file($file,$way);
If somebody can detect the error, inform me please
You need to change the last line to:
move_uploaded_file($file, $way);
If you look at the docs for move_uploaded_file, you will see that the first parameter is the place where the file is currently. Aka $_FILES['photo']['tmp_name'] which you have saved as $file.
Change the last line to:
move_uploaded_file($file, $way);