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;
}
Related
I have a form:
<form action='' enctype="multipart/form-data" method="post">
<input type="file" name="image">
<input type="submit" value="send">
</form>
I have php code:
$file = $_FILES['image']
$ext = explode(",", $file['type'])[0];
$location = "../image/movedimage.$ext";
if(move_uploaded_file($file['tmp_name'], $location)) echo 'moved';
else echo 'internal error';
This echos "moved" but the problem is that when I check the path to which the file was moved, the image file in there is corrupted.
I had to change the system of uploading the image by doing this:
$file_content = file_get_contents($file['tmp_name']);
$file_dump = file_put_contents($location, $file_content);
This attempt of placing the file directly using the file_put_contents works fine and the image file is perfect just as uploaded but using the move_uploaded_file leaves a corrupted file in the destination folder. I would like to understand why this is happening as the $file['error'] returns a value 0 and the move_uploaded_file function does not return false.
In your code by using
$ext = explode(",", $file['type'])[0];
you get the extension as image/your_image_type.
Then you are appending that with the file name, which will create an invalid image.
To get the extension, you can do as follows
$ext= explode("/", $file['type'])[1];
or
$ext = strtolower(end(explode('.',$_FILES['image']['name'])));
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;
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'm trying to move my uploaded file to a pictures folder. I dont get any errors when it comes to the script. Im using godaddy as the host. All file permissions are set up correctly. Have really no idea what else to do.
This is the php code:
<?php
public function CheckPicture($picture){
if(empty($_FILES['picture']['name'])){
echo "Must choose a file.";
}else{
$allowed = array('jpg', 'jpeg', 'png');
$file_name = $_FILES['picture']['name'];
//line 157->$file_extn = strtolower(end(explode('.', $file_name)));
$file_temp = $_FILES['picture']['tmp_name'];
if(in_array($file_extn, $allowed)){
$this->UploadPicture($username, $file_name, $file_extn);
}else{
echo $file_extn;
echo "Incorect file type. Types allowed: ";
echo implode(', ' , $allowed);
}
}
}
public function UploadPicture($username, $file_temp, $file_extn){
ini_set('display_errors',1);
error_reporting(E_ALL);
$file_path = '/home/content/49/11554349/html/gb/dev/images/pictures/' . substr(md5(time()), 0 , 9) . '.' . $file_extn;
move_uploaded_file($file_temp, $file_path);
echo $file_path;
print_r("$file_temp");
}
?>
This is how I am calling it in the html:
<?php
session_start();
include_once('post.php');
$username = unserialize($_SESSION["username"]);
$email = $_SESSION["email"];
if(!$_SESSION["username"]){
header("Location: http://www.greenboardapp.com/dev/");
}
if(isset($_FILES['picture'])){
$upload = new Post();
$upload->CheckPicture($picture);
}
?>
This is the form:
<div class="tile">
<img src="images/profileimg.png" alt="Tutors" class="tile-image">
<form action="profile.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="picture"><br>
<h6><input type="submit" value="Change Profile Pic" class="btn btn-hg btn-success"></h6>
</form>
</div>
The problem is, that end requires a reference, because it modifies the internal representation of the array (it makes the current element pointer point to the last element).
The result of explode('.', $file_name) cannot be turned into a reference. This is a restriction in the PHP language, that probably exists for simplicity reasons.
Output for 5.1.0 - 5.5.6
Strict Standards: Only variables should be passed by reference
Output for 5.0.5
Fatal error: Only variables can be passed by reference
Process exited with code 255.
Output for 4.3.0 - 5.0.4
Success
Solution
Find:
$file_extn = strtolower(end(explode('.', $file_name)));
$file_temp = $picture['tmp_name'];
Change to:
$file_extn_ex = explode('.', $file_name);
$file_extn_end = end($file_extn_ex);
$file_extn = strtolower($file_extn_end);
$file_temp = $picture['tmp_name'];
I have a problem to find out the relative path to
upload the images to other directories
target directory
htdocs/dt3/tadi/adm/dim/dim_images/tadi_user_images/
source directory
htdocs/dt3/tadi/adm/cbt/uploadfile.php
uploadfile.php
<html>
<form method="post"enctype="multipart/form-data">
<input type="file" name="fileimg1" ><input type="submit" name="upload">
</form>
</html>
<?php
$dirname = "/dt3/tadi/adm/dim/dim_images/tadi_user_images/";
$of = $_FILES['fileimg1']['name'];
$ext = pathinfo($of, PATHINFO_EXTENSION);
$changename3 = time() * 24 * 60;
$image_name3 = "timage_" . $changename3 . "." . $ext;
$final_pathdir = $dirname . $image_name3;
$suc = move_uploaded_file($_FILES['fileimg1']['tmp_name'], $final_pathdir);
if ($suc > 0)
{
echo "Image uploaded successfully";
}
else
{
echo "Error : " . $_FILES['filimg1']['error'];
}
?>
I tried out some of the paths, I didnt got any solution for that.
How can I upload images to that path?
htdocs/dt3/tadi/adm/dim/dim_images/tadi_user_images/
With relative path only.
I believe you could do
$dirname = dirname(__FILE__)."/../dim/dim_images/tadi_user_images/"