PHP:Uploaded File not move to folder (CLOSED) - php

Hello i have one problem to ask. The problem is when i'm trying to upload an image to certain folder usin php, the image is not move.The image information is insert into database but only the image is not move to destination folder. The folder is empty but no error is show. My file_upload in Php.ini is on but still the image is not move.
Below is my code:
<form action='try1.php' method='post' enctype='multipart/form-data'>
<table border='1'><tr>
<td><input type='file' name='file_img' /></td><td>
<input type='submit' name='btn_upload' value='upload'></td></tr>
</table>
</form>
<?php
require 'conf.php';
$link = mysqli_connect($h,$u,$p,$db);
if(isset($_POST['btn_upload']))
{
$filetmp = $_FILES["file_img"]["tmp_name"];
$filename = $_FILES["file_img"]["name"];
$filetype = $_FILES["file_img"]["type"];
$filepath = "upload/".$filename;
move_uploaded_file($filetmp,$filepath);
$query = "insert into try (image,type,path) values ('$filename','$filetype','$filepath')";
$result = mysqli_query($link,$query);
}
?>
I hope some one can help me to solve this problem,thank you.
(solved)I have solved the problem. its not the code but my pc that have problem.Thank you for helping.

At first delete your upload folder. After that
Please keep below code in your code.
$filepath = "upload/";
$filePathWithFileName = "upload/".$filename;
if (!file_exists($filepath)) {
mkdir($filepath, 0777);
}
move_uploaded_file($filetmp,$filePathWithFileName);

$filepath = "./upload/".$filename;

Related

PHP File Upload to Database

<body>
<form action="#" enctype="multipart/form-data" method="post">
<input multiple="" name="img[]" type="file" />
<input name="submit" type="submit" />
</form>
<?php
mmysql_connect("localhost","root","");
mysql_select_db("multiple");
if(isset($_POST['submit'])){
$filename = $_FILES['img']['name']);
$tmpname = $_FILES['img']['tmp_name']
$filetype = $_FILES['img']['type'];
for($i=0; $i<=count($tmpname)-1; $i++){
$name =addslashes(filename[$i]);
$tmp = addslashes(file_get_contents($tmpname[$i]));
mysql_query("INSERT into img(name,image) values('$name','$tmp')");
echo "uploaded";
}
}
?>
</body>
</html>
I am trying to upload a simple image to my database So I can work on this user hosted site. So far nothing has worked. I'm dyin here. I've looked through so many tutorials.
From hitting an issue with file uploads before, it's much easier on the DB and your coding to upload your file to the server using 'move_uploaded_file()' and then provide a reference in your DB. In the above example, you are storing your file in a temporary folder ['tmp_name'], saving that file name, but then not transferring the file somewhere that won't delete it.
So for something generic to help:
$fileName $_FILES['img']['name'];
$fileType = $_FILES['img']['type'];
$fileLocation = $_FILES['img']['tmp_name'];
$newFileName = "newFileName.jpg";
$img = "folder/".$newFileName;
move_uploaded_file($fileLocation, "../folder/".$newFileName);
The reason for this is that when you save to the 'tmp' folder (which you must for a bit), the file is also renamed to a seemingly random set of characters. So you need to get that file location, move it to the folder of your choice, and also name it something findable. Then you can save the $img path in your DB and call it from anywhere (with some modification). It will take some playing with, but this should help.
To upload an image to the database directly from an uploaded file, you need to upload the base64 encoded version of the image. And also make sure that the field type of the image in your database is actually blog or LongBlog.
Here is the code to do that
$file = $_FILES['file'];
$filename = $file['name'];
$convert_to_base64 = base64_encode(file_get_contents($file['tmp_name']));
$base64_image = "data:image/jpeg;base64,".$convert_to_base64;
$query = mysqli_query($con, "INSERT INTO 'table'(image)VALUES('$base64_image')")

0 kb when upload and store image to server

I have a table named images. It has two columns id and image (varchar300). I have this code upload.php :
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$image = $_POST['image'];
require_once('dbConnect.php');
$sql ="SELECT id FROM images ORDER BY id ASC";
$res = mysqli_query($con,$sql);
$id = 0;
while($row = mysqli_fetch_array($res)){
$id = $row['id'];
}
$path = "uploads/$id.png";
$actualpath = "/home/kinanday/public_html/$path";
$sql = "INSERT INTO images (image) VALUES ('$actualpath')";
if(mysqli_query($con,$sql)){
file_put_contents($path,base64_decode($image));
echo "Successfully Uploaded";
}
mysqli_close($con);
}else{
echo "Error";
}
this is upload.html :
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="image" />
<button>Upload</button>
</form>
</body>
The problem is: it saves into my database but the image in my file manager in Cpanel is 0kb
I am using this code to integration to my android app which need an image with varchar type. So in my app does not show image anything because the image is 0KB.
Can anybody with your advance to fix my php. every answer is very helpful for me. Thanks in advance.
You need to use the $_FILES superglobal, as #Fred-ii- said.
but $image = $_FILES['image'] is a array, see the options for it here: http://php.net/manual/en/features.file-upload.post-method.php
Examples:
$image_name = $_FILES['image']['name'];
$image_size = $_FILES['image']['size'];
$image_tmpname = $_FILES['image']['tmp_name']; // this is a temporary name that php uses on every upload he does.
$image_type = $_FILES['image']['type'];
$image_error = $_FILES['image']['error'];

How to upload excel file to php server from <input type="file">

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)

php file upload works locally not on server

Hi I am going from a xamp server on windows to a cpanel linux hosted server online.
My image upload script isn't working. I have set file perms on all files involved to 777 and that still doesn't help.
It seems the $_FILES['image1'] is empty.
Any ideas on what may cause this?
newProduct.php
<form action="newProduct.CREATE.php" method="post" enctype="multipart/form-data">
<input type="file" name="image1" id="image1" />
newProduct.CREATE.php
$directory = "img/products/";
$image = $_FILES['image1'];
$image1 = uploadImage($image, $directory);
Functions file:
function uploadImage($image, $directory) {
$file_name = $image['name'];
$file_size = $image['size'];
$file_tmp = $image['tmp_name'];
$file_type= $image['type'];
echo $image['name'];
$newName = convertFileToMD5($file_name);
move_uploaded_file($file_tmp,"$directory".$newName);
$image = "$directory" . $newName;
return $image;
}

move uploaded files into a 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);

Categories