Upload video from iPad - PHP - php

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 :)

Related

Generate thumbnail image from video in codeigniter

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

PHP - How to upload file uri from mobile device to web server?

I'm trying to build an mobile app that is capable to upload images to server. However, there's some problem with my php script, when I upload data URI of a screenshot, it works fine, but when I upload file URI of an image, the image uploaded into web server is broken. Can anyone help me to check what is wrong with the script?
<?php
//Generate a folder if it doesn't exist
if (!file_exists('user_images/'))
{
mkdir('user_images', 0755, true);
}
//Get submitted data by user
if (isset($_POST['CanvasPic'])) {
$img1 = $_POST['CanvasPic'];
} else {
$img1 = '';
}
if (isset($_POST['name'])) {
$name = $_POST['name'];
} else {
$name = '';
}
define('UPLOAD_DIR', 'user_images/');
//Remove some useless string before to create image properly
//Decode base64 then generate new name
if ($img1) {
$img1 = str_replace('data:image/jpeg;base64,', '', $img1);
$img1 = str_replace(' ', '+', $img1);
$data1 = base64_decode($img1);
$file1 = UPLOAD_DIR . $name . "_" . uniqid() . '.jpeg';
$success = file_put_contents($file1, $data1);
}
//Output a URL where is saved.
$locator = $file1;
//Display a text, otherwise if error happened.
print $locator ? $file1 : 'Could not save the file!';
?>
I think you php $_FILES superglobals array it is better to file or image uploading using php script to mobile device.
base64_decode()- it time consuming process to upload file to server.
Try below code.
$name = isset($_POST['name']) ? $_POST['name'] : '';
$uploadFileName = $name."_".uniqid().".jpeg";
define('UPLOAD_DIR', 'user_images/');
if (move_uploaded_file($_FILES['CanvasPic']['tmp_name'],UPLOAD_DIR.$uploadFileName)) {
echo "Uploaded.";
} else {
echo "File was not uploaded.";
}

php file upload different types of files

I'm trying to upload files in php using the following function :
public function fileUpload($FILES){
$num_of_uploads = 1;
$max_file_size = 1048576; //can't be larger than 1 MB
$T = array ();
foreach($_FILES["file"]["error"] as $key=>$value){
if($_FILES["file"]["name"][$key] != ""){
if($value == UPLOAD_ERR_OK){
$v = array ();
$origfilename = $_FILES["file"]["name"][$key];
$filename = explode(".", $_FILES["file"]["name"][$key]);
$filenameext = $filename[count($filename) - 1];
$v['name'] = $filename[0];
$v['extension'] = $filename[1];
$v['type'] = $_FILES["file"]["type"][$key];
unset($filename[count($filename) - 1]);
$filename = implode(".", $filename);
$filename = "file__" . time() . "." . $filenameext;
if($_FILES["file"]["size"][$key] < $max_file_size){
$v['content'] = file_get_contents($_FILES["file"]["tmp_name"][$key]);
$T[] = $v;
}else{
throw new Exception($origfilename . " file size inaccepted!<br />");
}
}else{
throw new Exception($origfilename . " Error of upload <br />");
}
}
}
return $T;
}
This function works great with txt types, but when I'm testing pdf, or gif or jpg, it returns a damaged file.
As far as I know, file_get_contents() works well on text/html types.
However, for other file types you should parse their text content first to use it in further processing. Try opening any .pdf in Notepad to see it's text content.
For uploading purposes, use move_uploaded_file() in your cycle, like this:
move_uploaded_file($_FILES["file"]["tmp_name"][$key], $filename);
Of course, without trying to get text content from uploaded file.
For downloading the file, you need to set headers. So, at the starting of function try setting any of below header for png or jpeg files:
//For png file
header("Content-Type: image/png");
//For jpeg file
header("Content-Type: image/jpeg");

joomla 2.5 system uploader variable

Using the joomla 2.5 uploader var
and this code does not work somewhere in here is a error and my debugger does not catch it
was wondering if someone else has a joomla debugg form that will address this and fix the problem..
function fileUpload($max, $module_dir, $file_type, $msg){
//Retrieve file details from uploaded file, sent from upload form
$file = JRequest::getVar('image', null, 'files', 'array');
if(isset($file)){
//Clean up filename to get rid of strange characters like spaces etc
$filename = JFile::makeSafe($file['name']);
if($file['size'] > $max) $msg = JText::_('ONLY_FILES_UNDER').' '.$max;
//Set up the source and destination of the file
$src = $file['tmp_name'];
$dest = $module_dir . DS .$filename;
//First check if the file has the right extension, we need jpg only
if ($file['type'] == $file_type | | $file_type == '*') {
if ( JFile::upload($src, $dest) ) {
//Redirect to a page of your choice
$msg = JText::_('FILE_SAVE_AS').' '.$dest;
} else {
//Redirect and throw an error message
$msg = JText::_('ERROR_IN_UPLOAD');
}
} else {
//Redirect and notify user file is not right extension
$msg = JText::_('FILE_TYPE_INVALID');
}
$msg = "<script>alert('". $msg ."');</script>";
}
return $msg;
}

Image upload takes previous temp file to upload in PHP

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.)

Categories