I am having trouble deleting the files with jquery fileupload . by clicking delete the error happens , do not delete the file . Error message:
upload/server/php/index.php?file=xxxx.png 406 (Not Acceptable)
Hpierce This is the code to which it refers
public function delete($print_response = true){
$file_names = $this->get_file_names_params();
if (empty($file_names)) {
$file_names = array($this->get_file_name_param());
}
$response = array();
foreach($file_names as $file_name) {
$file_path = $this->get_upload_path($file_name);
$success = is_file($file_path) && $file_name[0] !== '.' && unlink($file_path);
if ($success) {
foreach($this->options['image_versions'] as $version => $options) {
if (!empty($version)) {
$file = $this->get_upload_path($file_name, $version);
if (is_file($file)) {
unlink($file);
}
}
}
}
$response[$file_name] = $success;
}
return $this->generate_response($response, $print_response);
}
I have the same problem
Related
I am trying to use allow my custom API endpoint to upload files to a custom directory based on information sent in the request body. It is coming through fine but I am not getting it to pass into the directory properly. I have tried getting the studentid from the request body and then calling that as a global in my function but it is not working.
add_filter("wcra_upload_callback", "wcra_upload_callback_handler");
function wcra_upload_callback_handler($request) {
if (!function_exists('wp_handle_upload')) {
require_once(ABSPATH.'wp-admin/includes/file.php');
}
$studentid = $request['studentid'];
function studentresultsdir($dir) {
global $studentid;
$mydir = 'https://example.com/wp-content/uploads/studentresults/';
$dir['path'] = $mydir;
$dir['url'] = $mydir;
$dir['subdir'] = $studentid;
var_dump($dir);
return $dir;
}
add_filter("upload_dir", "studentresultsdir");
$uploadedfile = $_FILES['file'];
$upload_overrides = array('test_form' => false);
$movefile = wp_handle_upload($uploadedfile, $upload_overrides);
if ($movefile && !isset($movefile['error'])) {
echo __('File is valid, and was successfully uploaded.', 'textdomain')."\n";
var_dump($movefile);
} else {
echo $movefile['error'];
}
remove_filter("upload_dir", "studentresultsdir");
}
My var_dump of $dir is giving me an empty subdirectory. I think this is the cause of my "unable to create directory" error too but need to work this step out first.
Just wanted to add that I will be adding authentication checks once I get this working.
I managed to solve this particular error by moving the global variable outside of all functions.
add_filter("wcra_upload_callback", "wcra_upload_callback_handler");
$studentid = '';
function wcra_upload_callback_handler($request) {
if (!function_exists('wp_handle_upload')) {
require_once(ABSPATH.'wp-admin/includes/file.php');
}
global $studentid;
$studentid = $request['studentid'];
function studentresultsdir($dir) {
global $studentid;
$mydir = 'https://example.com/wp-content/uploads/studentresults/';
$dir['path'] = $mydir;
$dir['url'] = $mydir;
$dir['subdir'] = '/'.$studentid;
var_dump($dir);
return $dir;
}
add_filter("upload_dir", "studentresultsdir");
$uploadedfile = $_FILES['file'];
$upload_overrides = array('test_form' => false);
$movefile = wp_handle_upload($uploadedfile, $upload_overrides);
if ($movefile && !isset($movefile['error'])) {
echo __('File is valid, and was successfully uploaded.', 'textdomain')."\n";
var_dump($movefile);
} else {
echo $movefile['error'];
}
remove_filter("upload_dir", "studentresultsdir");
}
I can't seem to understand why the file is not downloading while it really exists.
Here's my code.
fileDownload.PHP
use \Logic\FTPConnection;
$ftpPath= 'path/to/file.txt';
$localPath = 'temp/file.txt';
$objFTPCon = new FTPConnection(FTP_HOST, FTP_USER, FTP_PASS);
$objFTPCon->openConnection();
$exist = $objFTPCon->isFileExist($ftpPath); // this returns as true
$objFTPCon->downloadFile($localPath , $ftpPath); // not downloading
FTPConnection.php
public function openConnection() // DONE
{
if (!$this->ftpOpen) {
$this->objConn = new SFTP($this->strHost);
if ($this->objConn === false) {
throw new \Exception('Unable to connect to SFTP server.');
}
if (!$this->objConn->login($this->strUser, $this->rsa)) {
throw new \Exception('Unable to login to SFTP server.');
}
$this->ftpOpen = true;
}
}
public function isFileExist($p_strFile)
{
$this->openConnection();
$strStandardizedPath = $this->standardizePath($p_strFile);
$blnFileExist = false;
$strFile = substr($strStandardizedPath, strrpos($strStandardizedPath, '/') + 1);
$strPath = substr($strStandardizedPath, 0, strrpos($strStandardizedPath, '/'));
if ($this->isPathExist($strPath)) {
$arrFileList = $this->objConn->nlist($strPath);
if ($arrFileList) {
if (in_array($strFile, $arrFileList)) {
$blnFileExist = true;
}
}
}
return $blnFileExist;
}
public function downloadFile($p_strTargetFile, $p_strSourceFile)
{
$this->openConnection();
// $this->objConn->get($p_strTargetFile, $p_strSourceFile, FTP_BINARY);
if (!$this->objConn->get($p_strTargetFile, $p_strSourceFile, FTP_BINARY)) {
throw new \Exception('Unable to download file.');
}
}
It appears that my parameters for get() is wrong.
I just swapped it and it works:
so instead of:
$this->objConn->get($p_strTargetFile, $p_strSourceFile, FTP_BINARY);
i used:
$this->objConn->get($p_strSourceFile, $p_strTargetFile);
Hi im creating a project in laravel 4.2 that uploads different files (e.g Pdf and images) in specific folders about 8 or 9 folders to be exact i just try the do a if statement but dont know what happend but its not working and there are no error displayed when i execute the code. please help me work this code or do you have any idea to simplify my code TIA!
Here's my code in my controller
public function upload()
{
$finame = Input::get('name');
$ftype = Input::get('type');
$username = Input::get('user');
$desc = Input::get('desc');
$date = Input::get('date');
$upload = Input::file('file');
$add = Files::upload2($finame,$ftype,$username,$desc,$date,$upload);
if ($add == true && $ftype == "pf") {
$upload -> move(public_path('pf'),$upload->getClientOriginalName());
return \Response::json(array('success' => true));
return Redirect::to('/home')->with('message', 'message|Record Successfully Added.');
} elseif ($add == true && $ftype == "sf") {
$upload -> move(public_path('sf'),$upload->getClientOriginalName());
return \Response::json(array('success' => true));
return Redirect::to('/home')->with('message', 'message|Record Successfully Added.');
} } elseif ($add == true && $ftype == "gale") {
$upload -> move(public_path('gale'),$upload->getClientOriginalName());
return \Response::json(array('success' => true));
return Redirect::to('/home')->with('message', 'message|Record Successfully Added.');
} } elseif ($add == true && $ftype == "advisory") {
$upload -> move(public_path('advisory'),$upload->getClientOriginalName());
return \Response::json(array('success' => true));
return Redirect::to('/home')->with('message', 'message|Record Successfully Added.');
} else {
return Redirect::back()->with('message', 'error|Error');
}
}
And my code in model:
public static function upload2($finame,$ftype,$username,$desc,$date,$upload)
{
$files = new self;
$files->file_name = $finame;
$files->file_type = $ftype;
$files->username = $username;
$files->description = $desc;
$files->date = $date;
$files->upload = $upload->getClientOriginalName();
try {
$files->save();
} catch (Exception $e) {
dd($e);
}
}
and in the view add this script:
<script>
var form = document.getElementById('upload');
var request = new XMLHttpRequest();
form.addEventListener('submit', function(e){
e.preventDefault();
var formdata = new FormData(form);
request.open('post', '/upload');
request.addEventListener("load", transferComplete);
request.send(formdata);
});
function transferComplete(data){
response = JSON.parse(data.currentTarget.response);
if(response.success){
document.getElementById('message').innerHTML = "File Successfully Uploaded!";
}
}
</script>
Earlier I have a form that ask user to upload a picture and I have this function:
function fileUploaded() {
$fileName = $_FILES ['picture'] ['name'];
$pathOfFile = "/images/";
$fileTmpLoc = $_FILES ['picture'] ["tmp_name"];
$fileResult = move_uploaded_file ( $fileTmpLoc, $pathOfFile );
if (isset ( $fileName )) {
return true;
}
}
Basically it moves the uploaded picture to images file. Then I am calling this function in an if statement:
if (fileUploaded () == true) {
if ($fileResult) {
/*checking the size of file*/
}
}
else {
$fileName = "default.jpg";
}
After when I try to upload and submit it gives the error in the below:
Fatal error: Call to undefined function fileUploaded()
What should be the problem?
Thanks.
You don't return a default value in your function. Maybe it's the problem :
function fileUploaded() {
$fileName = $_FILES ['picture'] ['name'];
$pathOfFile = "/images/";
$fileTmpLoc = $_FILES ['picture'] ["tmp_name"];
$fileResult = move_uploaded_file ( $fileTmpLoc, $pathOfFile );
if (isset ( $fileName )) {
return true;
}
return false;
}
//functions.php
function fileUpload($path) {
if(!isset($_FILES['picture'])) return false;
$fileName = $_FILES['picture']['name'];
$fileTmpLoc = $_FILES['picture']['tmp_name'];
if(move_uploaded_file ($fileTmpLoc, $path)) return $fileName;
return false;
}
//main.php
include('functions.php');
$fileName = fileUpload('/images/');
if($fileName === false) {
$fileName = 'default.jpg';
}
//do the rest here
Something similar to the above code. Since your function is in a different file, you need to include it (or require it)
hi all when Using exif_imagetype() [function.exif-imagetype]: function for checking images if the user hits the submit button without uploading anything the exif function returns an error in the webpage itself. my question is how to get rid of this error. am pasting the error below
Warning: exif_imagetype() [function.exif-imagetype]: Filename cannot be empty in /mounted- storage/home98a/sub009/sc61374-HGPS/sitakalyanam.com/newsita/php4upload.class.php on line 88
<?php
/*
- PHP4 Image upload script
*/
class imageupload
{
//pblic variables
var $path = '';
var $errorStr = '';
var $imgurl = '';
//private variables
var $_errors = array();
var $_params = array();
var $_lang = array();
var $_maxsize = 1048576;
var $_im_status = false;
//public methods
function imageupload ()
{
//require 'photouploadconfig.php';
if($_GET['Choice']=="1")
{
require 'Photouploddir1.php';
}
elseif ($_GET['Choice']=="2")
{
require 'Photouploddir2.php';
}
elseif ($_GET['Choice']=="3")
{
require 'Photouploddir3.php';
}
elseif ($_GET['horoschoice']=="1")
{
require 'horosuploaddir.php';
}
elseif ($_GET['videoChoice']=="5")
{
require 'videouploaddir.php';
}
$this->_types = $types;
$this->_lang = $lang;
$this->_upload_dir = $upload_dir;
$this->_maxsize = $maxsize;
$this->path = $PHP_SELF;
if (is_array($_FILES['__upload']))
{
$this->_params = $_FILES['__upload'];
if (function_exists('exif_imagetype'))
$this->_doSafeUpload();
else
$this->_doUpload();
if (count($this->_errors) > 0)
$this->_errorMsg();
}
}
function allowTypes ()
{
$str = '';
if (count($this->_types) > 0) {
$str = 'Allowed types: (';
$str .= implode(', ', $this->_types);
$str .= ')';
}
return $str;
}
// private methods
function _doSafeUpload ()
{
preg_match('/\.([a-zA-Z]+?)$/', $this->_params['name'], $matches);
if (exif_imagetype($this->_params['tmp_name']) && in_a rray(strtolower($matches[1]), $this->_types))
{
if ($this->_params['size'] > $this->_maxsize)
$this->_errors[] = $this->_lang['E_SIZE'];
else
$this->_im_status = true;
if ($this->_im_status == true)
{
$ext = substr($this->_params['name'], -4);
$this->new_name = md5(time()).$ext;
move_uploaded_file($this->_params['tmp_name'], $this->_up load_dir.$this->new_name);
$this->imgurl =$this->new_name;
//$this->imgurl = .$this->new_name;
}
}
else
$this->_errors[] = $this->_lang['E_TYPE'];
}
function _doUpload ()
{
preg_match('/\.([a-zA-Z]+?)$/', $this->_params['name'], $matches);
if(in_array(strtolower($matches[1]), $this->_types))
{
if ($this->_params['size'] > $this->_maxsize)
$this->_errors[] = $this->_lang['E_SIZE'];
else
$this->_im_status = true;
if ($this->_im_status == true)
{
$ext = substr($this->_params['name'], -3);
$this->new_name = md5(time()).$ext;
move_uploaded_file($this->_params['tmp_name'], $this- >_upload_dir.$this->new_name);
$this->imgurl = ''.$this->new_name;
//$this->imgurl = ''.$this->_upload_dir.''.$this->new_name;
//$this->imgurl = ''.$this->new_name;
//$this->imgurl = $this->_upload_dir.'/'.$this->new_name;
}
}
else
$this->_errors[] = $this->_lang['E_TYPE'];
}
function _errorMsg()
{
$this->errorStr = implode('<br />', $this->_errors);
}
}
?>
You are getting that message because you are never checking if the user uploaded a file or not, you're just assuming they are. When the user does not upload a file then $_FILES will be an empty array.
That means that $this->_params['tmp_name'] won't exist. You need to check if a file was uploaded, not just assume one was.
Just simply check the size of $_FILES.
if(count($_FILES) === 0){
echo "no file uploaded";
}
Change your code to this one and then try
if (isset($_FILES['__upload']))
{
$this->_params = $_FILES['__upload'];
if (function_exists('exif_imagetype'))
$this->_doSafeUpload();
else
$this->_doUpload();
if (count($this->_errors) > 0)
$this->_errorMsg();
}