Here I have the problem with uploading image in PHP.
The problem is that when first time I upload the image file it works fine.
But when I am trying to upload the file second time without page refresh it takes first image name and upload it.
What is the problem and how can it be resolved?
$name = $_FILES['ImageFile']['name'];
$size = $_FILES['ImageFile']['size'];
$tmp = $_FILES['ImageFile']['tmp_name'];
$path = "public/www/uploads/";
$valid_formats = array("jpg", "png", "gif", "bmp");
$response = '';
if(strlen($name)) {
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats)) {
if($size<(1024*1024)) {
$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
if(move_uploaded_file($tmp, $path.$actual_image_name)) {
$response = "<img src='public/www/uploads/".$actual_image_name."?parm=".time()."' class='preview'>";
} else {
$response = "failed";
}
} else {
$response = "Image file size max 1 MB";
}
} else {
$response = "Invalid file format..";
}
} else {
$response = "Please select image..!";
}
Here, $response is a variable that used to get status.
Sounds like you are using some sort of AJAX to call this function.
You might need to find a way to reset the $_FILES array at the end of this function... maybe something like this would help:
$_FILES = array();
Otherwise, because there is no (apparent) page refresh happening after the file upload (as you mentioned,) I'm thinking that the $_FILES variable has no chance of being naturally reset (as would happen if you weren't using AJAX here.)
Related
i want to generate the thumbnail image from the uploaded video but the problem is that the thumbnail image is not generating. The uploaded video goes to the uploads folder but at this place the image is not generating..Please look at the code, and tell me where i am wrong.
public function add_video() { // move_upload_file code
if(!empty($_FILES['video']['name'])){
$tmp_name_array = $_FILES['video']['tmp_name'];
$n_array = $_FILES['video']['name'];
$exp = explode('.', $n_array);
$newnme = date('his').rand().'.'.end($exp);
$raw_name = explode('.', $newnme);
$full_path = base_url('uploads').'/'.$newnme;
$new_path = base_url('uploads').'/';
if(move_uploaded_file($tmp_name_array, "uploads/".$newnme))
{
$full_path = base_url('uploads').'/'.$newnme;
$new_path = base_url('uploads').'/';
print_r(exec("ffmpeg -i ".$full_path." ".$new_path.$raw_name[0].".jpg"));
echo "uploaded Successfully";
}
}else{
echo "not selected any file";
}
}
Stuck on some weird errors. Created an ipad web app to upload video from ipad gallery to my web server. Showing invalid file format, same video directly from desktop browser, working fine. Is there any issue with iPad or any mime type issue. See my code.
<?php
ini_set('max_execution_time', 2400);
$path = "uploads/";
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
$valid_formats = array("mp4", "MP4","jpg","avi","AVI");
list($txt, $ext) = explode(".", $name);
$errors = array();
$form_data = array();
if(!empty($name))
{
if(in_array($ext,$valid_formats))
{
if ($size<(1024*1024*1024*1024*1024*1024*1024*1024*1024*1024*1024*1024*1024*1024))
{
$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
} else {
$errors['imgsize'] = 'Image file size max issue.<br/>';
}
} else{
$errors['imgformat'] = 'Invalid file format..<br/>';
}
}
if (!empty($errors)) {
$form_data['success'] = false;
$form_data['errors'] = $errors;
} else {
$form_data['success'] = true;
$form_data['posted'] = '1';
move_uploaded_file($tmp, $path.$actual_image_name);
}
//Return the data back to form.php
header('Content-type: application/json');
echo json_encode($form_data);
?>
I faced the similar issue, what ipad/iphone does it compresses & change the format to MOV whatever the format is. If you are uploading mp4, it will convert to MOV. Since your code does not check MOV format so its throwing an error. Add mov format and it will work. You are welcome :)
the following piece of code recognizes the image through getimagesize() but then when i try to move the file to an uploaded folder it moves the file there but says it's an array? im confused because im not setting any variables as an array?
<?php
//simple image check using getimagesize() instead of extensions
if($_FILES){
$empty_check = getimagesize($_FILES['file']['tmp_name']);
if(empty($empty_check)){
echo 'this is not an image';
}
else{
echo 'you have uploaded ' . explode('.',$_FILES['file']['name'])[0].'
and it is a ' . explode('.',$_FILES['file']['name'])[1].'.';
//an example of how i would extract the extension
$target = "C:\\xampp\\htdocs";
move_uploaded_file($_FILES['file']['tmp_name'], $target.'\\'.$_FILES['file']);
}
}
?>
$_FILES['file']
is an array, you're trying to use it as the target filename;
comment of deceze.
Echo the file you want to move/save, then you should see what he mentioned..
When using move_uploaded_file you get to pick the filename, so you can pick anything you want.
When you upload the file, its put into a temporary directory with a temporary name, move_uploaded_file() allows you to move that file and in that you need to set the name of the file as well.
Use this coding for multiple file uploading....
//For Multiple file uploading
if (isset($_FILES['photo']) != "") {
$errors = array();
foreach($_FILES['photo']['tmp_name'] as $key = > $tmp_name) {
$file_name = $_FILES['photo']['name'][$key];
$file_size = $_FILES['photo']['size'][$key];
$file_tmp = $_FILES['photo']['tmp_name'][$key];
$file_type = $_FILES['photo']['type'][$key];
//change the image extension as png
$fileExt = "png";
$photorename[$key] = strtolower($property_code.
'_'.$key.
'.'.$fileExt);
if ($file_size > 2097152) {
$errors[] = 'File size must be less than 2 MB';
}
//Path of Uploading file
$target = "images_property";
if (empty($errors) == true) {
if (is_dir($target) == false) {
mkdir("$target", 0700); // Create directory if it does not exist
}
if (file_exists("$target/".$photorename[$key])) {
unlink("$target/".$photorename[$key]);
}
move_uploaded_file($file_tmp, "$target/".$photorename[$key]);
} else {
print_r($errors);
}
}
if (empty($errors)) {
echo "Success";
}
}
I searched online for a code that does PHP image upload using ajax. I found the code attached below. The issue is that I changed few things (minor tweaks) to make it work on my server. Originally it was just a php page (not a class or function) that handles the data posted from form. I made it into class then function. I am following OOP now. I thought the best way to do things in the conversion from procedural to OOP was to pass $_FILES and $_POST to a method and inside deal with them. I think this didn't work. Look at the example and please advise on how to go forward.
function uploadImageChosen($_FILES, $_POST){
$path = "../uploads/images/";
$valid_formats = array("jpg", "png", "gif", "bmp");
$connectionInstance = new ConnectionClass();
$connectionInstance->connectToDatabase();
$imgName;
$imgURL;
$imgSize;
$imgDir = $_POST['directory'];
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['photoimg']['name'];
$imgSize = $_FILES['photoimg']['size'];
if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
if($size<(1024*1024))
{
$imgName = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$imgName))
{
$imgURL = $path.$imgName;
$connectionInstance->query("INSERT INTO imagesupload(id, title, url, size, directory) VALUES (null, '$imgName','$imgURL', '$imgSize', '$imgDir')");
//echo "<img src='uploads/".$imgName."' class='preview'>";
}
else{
echo "failed";
}
}else{
echo "Image file size max 1 MB";
}
}else{
echo "Invalid file format..";
}
}else{
echo "Please select image..!";
}
}//end of if
}//end of function
As to the page where the class function is being called, here it is:
<?php
require_once("../classes/UploadImages.php");
$uploadInstance = new UploadImages();
$uploadInstance->uploadImageChosen($_FILES, $_POST);
//header("LOCATION:portfolio.php");
?>
Thank you very much :)
$_POST and $_FILES are superglobal arrays, they are always available, and redefining them in a function or method is a bad idea.
You can do something like this:
$uploadInstance->uploadImageChosen();
..
function uploadImageChosen(){
$path = "../uploads/images/";
$valid_formats = array("jpg", "png", "gif", "bmp");
...
$name = $_FILES['photoimg']['name'];
...
Or if you need copies in the local scope do it like this:
$uploadInstance->uploadImageChosen($_FILES, $_POST);
..
function uploadImageChosen($files, $post){
$path = "../uploads/images/";
$valid_formats = array("jpg", "png", "gif", "bmp");
...
$name = $files['photoimg']['name'];
...
Try removing $_SERVER['REQUEST_METHOD'] == "POST" from the IF-statement, and see what it does.
The script above has a problem which was not detected in error log. The problem was first presented when I instantiated the connection class. I should have created another variable that would receive the open connection for querying to work That was solved and data now present in the DB. The second problem was the JPG format was not in array of acceptable types, so I added that along with jpg (variations). This made the file actually transfer to upload folder. Thank you all for support and sorry for the inconvenience :)
I doing an ajax file uploadusing jquery.form.js.
After uploading I send back the uploaded files' id from PHP to tpl(Smarty).
Problem is that now I'm getting the tpl twice. When I put an exit command in the php file I can solve this problem, but then I will not get the value of the returned file's id to tpl.
Now I would like to get the returned file key in tpl, and also want to display the tpl only once.
if(isset($input['act']) and trim($input['act']) == "uplaodFile") {
$valid_formats = array("jpg", "png", "gif", "bmp");
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
if($size<(1024*1024))
{
$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if($filekey = $files->uploadFile($contact_id, $wrkspace_id, $_FILES['photoimg'], "files", null))
{
$smarty->assign("filekey",$filekey);
}
}
else
Error::setError ("Image file size max 1 MB");
}
else
Error::setError("Invalid file format..");
}
else
Error::setError("Please select image..!");
//EXIT;
}
display("schedule_event");