[PHP][SLIM] move_uploaded_file never ends with larger files in Hosting24 - php

I have a question.
I'm trying to upload files to a server, this server is hosted by 'Hosting24', i don't know if that matters, anyway, if I try to move files larger than 70mb It never ends to load and never send a response, my server config is okay, it has these values: {"post_max_size":"512M","memory_limit":"1024M"}
I has tryed with $_FILES and ftp transfer and it doesnt works, it never ends of load.
I'm using Slim framework and this is the function:
public function testCode($request, $response, $args)
{
$data = $request->getParsedBody();
$uploadedFiles = $request->getUploadedFiles();
//here take files
$uploadedFile = $uploadedFiles['file_user'];
$nameC = $data['nombre_curso'];
//here set path
$path = "../../scorm/temp/" . $nameC . '/';
//Creating folders
if (!#mkdir($path, 0777, true)) {
$error = error_get_last();
echo $error['message'];
}
$file_name = $uploadedFile->getClientFilename();
$array = explode(".", $file_name);
$ext = $array[1];
//if is a zip file
if ($ext == 'zip') {
$location = $path . "/" . $file_name;
//here move the uploaded file to my selected directory
if (move_uploaded_file($uploadedFile->file, $location)) {
$obj = (object) array("response" => 'works', 'post_max_size' => ini_get('post_max_size'), 'memory_limit' => ini_get('memory_limit'));
$response->withStatus(200);
$response->getBody()->write(json_encode($obj));
return $response;
} else {
$error = error_get_last();
echo $error['message'] . " it couldn't be moved";
}
} else {
echo "no zip";
}
}

Related

unzip file Failed in PHP wordpress

Hello guys i am trying to upload file and extraced in my plugin folder, The Uploading and moving function they works perfect but when the function try to extraced it's give error that it's failed to extraced the uploaded file where is the problem in my code?
i guess $zip->open is not working
here is my code
function rmdir_recursive($dir) {
foreach(scandir($dir) as $file) {
if ('.' === $file || '..' === $file) continue;
if (is_dir("$dir/$file")) rmdir_recursive("$dir/$file");
else unlink("$dir/$file");
}
rmdir($dir);
}
if(isset($_POST))
{
if($_FILES["file"]["name"]) {
$filename = $_FILES["file"]["name"];
$source = $_FILES["file"]["tmp_name"];
$type = $_FILES["file"]["type"];
$name = explode(".", $filename);
$accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
foreach($accepted_types as $mime_type) {
if($mime_type == $type) {
$okay = true;
break;
}
}
$continue = strtolower($name[1]) == 'zip' ? true : false;
if(!$continue) {
echo "The file you are trying to upload is not a .zip file. Please try again.";
}
/* PHP current path */
$path = dirname(__FILE__).'/packages/'; // absolute path to the directory where zipper.php is in
$filenoext = basename ($filename, '.zip'); // absolute path to the directory where zipper.php is in (lowercase)
$filenoext = basename ($filenoext, '.ZIP'); // absolute path to the directory where zipper.php is in (when uppercase)
$targetdir = $path . $filenoext; // target directory
$targetzip = $path . $filename; // target zip file
/* create directory if not exists', otherwise overwrite */
/* target directory is same as filename without extension */
if (is_dir($targetdir)) rmdir_recursive ( $targetdir);
mkdir($targetdir, 0777);
/* here it is really happening */
if(move_uploaded_file($source, $targetzip)) {
$zip = new ZipArchive; // create object
$res = $zip->open($targetzip); // open archive
if ($res === TRUE) {
$zip->extractTo($targetdir); // extract contents to destination directory
$zip->close();
echo 'extraced successed';
}else{
echo 'extraced failed';
}
echo "Your .zip file was uploaded and unpacked.";
} else {
echo "There was a problem with the upload. Please try again.";
}
}
}

How to remove sanitize from Opencart downloads

I am developing a module for my client to upload and browse file in Opencart.
when I am uploading file from my back-end server I am getting the output as file.zip.xyzasdf. Where I just want to remove this .xyzasdf
Can any one suggest me how to remove sanitize from the following code...
public function upload() {
$this->load->language('catalog/download');
$json = array();
// Check user has permission
if (!$this->user->hasPermission('modify', 'catalog/download')) {
$json['error'] = $this->language->get('error_permission');
}
if (!$json) {
if (!empty($this->request->files['file']['name']) && is_file($this->request->files['file']['tmp_name'])) {
// Sanitize the filename
$filename = basename(html_entity_decode($this->request->files['file']['name'], ENT_QUOTES, 'UTF-8'));
// Validate the filename length
if ((utf8_strlen($filename) < 3) || (utf8_strlen($filename) > 128)) {
$json['error'] = $this->language->get('error_filename');
}
// Allowed file extension types
$allowed = array();
$extension_allowed = preg_replace('~\r?\n~', "\n", $this->config->get('config_file_ext_allowed'));
$filetypes = explode("\n", $extension_allowed);
foreach ($filetypes as $filetype) {
$allowed[] = trim($filetype);
}
if (!in_array(strtolower(substr(strrchr($filename, '.'), 1)), $allowed)) {
$json['error'] = $this->language->get('error_filetype');
}
// Allowed file mime types
$allowed = array();
$mime_allowed = preg_replace('~\r?\n~', "\n", $this->config->get('config_file_mime_allowed'));
$filetypes = explode("\n", $mime_allowed);
foreach ($filetypes as $filetype) {
$allowed[] = trim($filetype);
}
if (!in_array($this->request->files['file']['type'], $allowed)) {
$json['error'] = $this->language->get('error_filetype');
}
// Check to see if any PHP files are trying to be uploaded
$content = file_get_contents($this->request->files['file']['tmp_name']);
if (preg_match('/\<\?php/i', $content)) {
$json['error'] = $this->language->get('error_filetype');
}
// Return any upload error
if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) {
$json['error'] = $this->language->get('error_upload_' . $this->request->files['file']['error']);
}
} else {
$json['error'] = $this->language->get('error_upload');
}
}
if (!$json) {
$file = $filename . '.' . token(32);
move_uploaded_file($this->request->files['file']['tmp_name'], DIR_FOLDER . $file);
$json['filename'] = $file;
$json['mask'] = $filename;
$json['success'] = $this->language->get('text_upload');
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
Any help would be greatly appreciated...
Thanks
Removing the random string that is added to the filename is simple. Just change
move_uploaded_file($this->request->files['file']['tmp_name'], DIR_UPLOAD . $file);
to:
move_uploaded_file($this->request->files['file']['tmp_name'], DIR_UPLOAD . $filename);
But keep in mind that this will bring problems.
OpenCart saves the random string in the database at the time of file upload, so it will later use it to identify the file.
If you delete this feature, the uploaded files in the admin panel will not be available.

file not found exception in laravel

I am facing some problem while uploading image in laravel framework of my project .I checked image is uploading to folder , but still this error is coming
Below is coding
/////////// Course Image ///////////
$tmp_name1 = $course_image['tmp_name'];
$type1 = $course_image['type'];
$name1 = $course_image['name'];
$res1 = $this->upload_file($tmp_name1, $name1);
if ($res1) {
$course_image_url = $res1;
} else {
$course_image_url = "";
}
function upload_file($source, $name) {
$list = explode(".", $name);
$ext = $list[count($list) - 1];
$ext = strtolower($ext);
if (in_array($ext, $this->file_extension)) {
$filename = md5(date("YmdHis") . microtime() . rand(100, 100000000)) . "." . $ext;
$destination = "public/uploads/" . $filename;
$res = move_uploaded_file($source, $destination);
if ($res) {
return $destination = "uploads/" . $filename;
} else {
$message = "Internal server error";
(json_encode(array("responseCode" => "500", "responseMsg" => array("status" => "error", "statusReason" => $message))));
}
} else {
$message = "Invalid file extention";
(json_encode(array("responseCode" => "500", "responseMsg" => array("status" => "error", "statusReason" => $message))));
}
}
There are lots of codes , I posted only the required one .
You should follow the Laravel Example for Uploading Files.
I.e., you can check if a file has been uploaded successfully using:
if ($request->hasFile('image')) {
//
}
And you can move / rename a file using:
$request->file('image')->move($destinationPath);
or
$request->file('image')->move($destinationPath, $fileName);
I found that issue .
Go to line number 66 of this path(D:\xampp\htdocs\blog\vendor\laravel\framework\src\Illuminate\Http\UploadedFile.php) in your project folder .or search the text instanceof static.
After it remove the "instanceof static" ,then it is working fine.
Thanx

Saving Multiple File path to mysql using PHP

Good Day. I have a php script that move multiple file in my directory..
$filepath = 'uploads/';
if (isset($_FILES['file'])) {
$file_id = $_POST['file_id'];
$count = 0;
foreach($_FILES['file']['tmp_name'] as $k => $tmp_name){
$name = $_FILES['file']['name'][$k];
$size = $_FILES['file']['size'][$k];
if (strlen($name)) {
$extension = substr($name, strrpos($name, '.')+1);
if (in_array(strtolower($extension), $file_formats)) { // check it if it's a valid format or not
if ($size < (2048 * 1024)) { // check it if it's bigger than 2 mb or no
$filename = uniqid()."-00000-". $name;=
$tmp = $_FILES['file']['tmp_name'][$k];
if (move_uploaded_file($tmp_name, $filepath . $filename)) {
$id = $file_id;
$file_path_array = array();
$files_path = $filepath . $filename;
$file_extension = $extension;
foreach($file_name as $k_file_path => $v_file_path){
$file_path_array[] = $v_file_path;
}
foreach($file_extension as $k_file_extension){
$file_extension_array[] = $v_file_extension;
}
$file_path = json_encode($files_path);
$file_name = str_replace("\/", "/",$file_path);
var_dump($file_name);
$update = $mysqli->query("UPDATE detail SET file_path='$file_name' WHERE id='$id'");
} else {
echo "Could not move the file.";
}
} else {
echo "Your file is more than 2MB.";
}
} else {
echo "Invalid file format PLEASE CHECK YOU FILE EXTENSION.";
}
} else {
echo "Please select FILE";
}
}
exit();
}
this is my php script that move file to 'uploads/' directory and i want to save the path to my database. i try to dump the $file_name and this is my example path how to save that to my database.. ? any suggestions ?
NOTE: i already move the file to uploads/ directory and i only want to save the path to my database
string(46) "uploads/5638067602b48-00000-samplePDF.pdf"
string(46) "uploads/5638067602dee-00000-samplePDF1.pdf"
string(46) "uploads/5638067602f8d-00000-samplePDF2.pdf"
if you must store them in one field..
inside the loop
$file_name_for_db[]=$file_name;
outside the loop:
$update = $mysqli->query("UPDATE detail SET file_path='".json_encode($file_name_for_db)."' WHERE id='$id'");
there is serialize() instead of json_encode() if you prefer

Zip not uploading :- (

For some reason when trying to upload a zip file this function always returns false. The directories are all set to 0777 for permissions. I'm stumped as to what could be wrong.
function uploadProof ( $file, $email )
{
// Check or create for existing directory
if ( !is_dir('client_files/'.$email))
{
mkdir('client_files/'.$email);
if ( !is_dir('client_files/'.$email.'/proof/'))
{
mkdir('client_files/'.$email.'/proof/');
}
}
// Target path
$target_path = 'client_files/'.$email.'/proof/';
// File information
$filename = date('Y_M_D').$email.'.zip';
$tmp_name = $file['tmp_name'];
$filesize = $file['size'];
// Blacklist and Max file info
$max_allowed = (1024 * 1024) * 99; // 99 MB
$blacklist = array(
'.pl', '.php', '.phtml', '.php3', '.php4', '.php5'
);
// Check filename
foreach ( $blacklist as $nope)
{
if ( preg_match("/$nope\$/i", $filename))
{
die("As previously stated, we do not allow php files of any type\n
to be uploaded to our server.\n\n");
}
}
// Check filesize
if ( $filesize > $max_allowed)
{
die("File is too big, file needs to be less than <em>20MB</em> in size.");
}
else
{
$target = $target_path.$filename;
if (move_uploaded_file($tmp_name, $target))
{
return true;
}
else
{
return false;
}
}
}
You do need to check if the upload actually succeeded, before going on to do what might be a totally useless set of operations:
function uploadProof ( $file, $email ) {
if ($file['error'] !== UPLOAD_ERR_OK) {
die("Upload failed with error code " . $file['error']);
}
...
}
The error codes are defined here.

Categories