imagick error: 'unable to open image' image upload - image-uploading

I get the following error when trying to process uploaded images with imagick.
Fatal error: Uncaught exception 'ImagickException' with message 'unable to open image `9eK59iu.jpg': No such file or directory # error/blob.c/OpenBlob/2644' in D:\PATH\upload.php on line 77
The code looks like this:
<?php
$new_folder_name = "D:/PATH/content";
mkdir("$new_folder_name",0700);
$tmp_img = $_FILES["upload_file"]["tmp_name"];
$img = new Imagick($tmp_img);
$img->thumbnailImage(100 , 100 , TRUE);
$img->writeImage($new_folder_name);
?>
Without imagick the image upload works just fine.
Only imagick won't open the image given to $_FILES
I also tried to open the image with imagick, after move_uploaded_file, like this:
<?php
$extension = pathinfo($upload_file_name, PATHINFO_EXTENSION);
$new_upload_file_name = rand(00000, 99999).".".$extension;
$new_folder_name = "D:/PATH/content".time();
mkdir("$new_folder_name",0700);
$path_to_file = $new_folder_name."/".$new_upload_file_name;
move_uploaded_file($_FILES["upload_file"]["tmp_name"],$path_to_file);
$img = new Imagick($path_to_file);
$img->thumbnailImage(100 , 100 , TRUE);
$img->writeImage($new_folder_name);
?>
neither works.. :-(
Any suggestion?

Read the file upload docs. The server-side temporary filename assigned by PHP to store the uploaded file in ['tmp_name'] in the $_FILES array. You're trying to use the client-side user-provided ['name'], which DOES NOT exist anywhere on your server.
$tmp_img = $_FILES["upload_file"]["tmp_name"];
^^^^
You are also simply assuming that the upload has succeed. That is NOT a good thing. Never EVER assume success with dealing with remote resources (web apis, file uploads, database operations, etc...). ALWAYS check for errors:
if ($_FILES['upload_file']['error'] !== UPLOAD_ERR_OK) {
die("Upload failed with error code " . $_FILES['upload_file']['error']);
}

Related

Autodesk Forge: adding image files Specified image protocol is invalid

I have created a photoscene in Autodesk Forge. Now I am now trying to upload files to it using this PHP code and my uploads are failing. You can check the urls of the images exist online, are jpgs, and should be acceptable to Forge. (I have also tried uploading them as local files and received the same error messages).
What's preventing Forge from accepting my images, and how do I fix it?
foreach( $files_for_forge as $url )
{
$curl_cmd =
"curl -s $FORGE_URL/photo-to-3d/v1/file " .
"-H 'Authorization: Bearer $access_token' " .
"-d 'photosceneid=$photosceneID' " .
"-d 'type=image' " .
"-d 'file[$filecounter]=$url' "
;
$json = shell_exec ( $curl_cmd );
echo "File $filecounter: $url => $json<br />";
$filecounter++;
}
The resulting output shows every file upload attempt is failing with this error: "Error":{"code":"18","msg":"Specified image protocol is invalid"}
File 0: cloud1.tri-di.com/scans/D-A000-5d008f27/original_images/img-13.jpg => {"Usage":"0.67387795448303","Resource":"\/file","Error":{"code":"18","msg":"Specified image protocol is invalid"}}
File 1: cloud1.tri-di.com/scans/D-A000-5d008f27/original_images/img-22.jpg => {"Usage":"0.70915198326111","Resource":"\/file","Error":{"code":"18","msg":"Specified image protocol is invalid"}}
File 2: cloud1.tri-di.com/scans/D-A000-5d008f27/original_images/img-12.jpg => {"Usage":"0.76431202888489","Resource":"\/file","Error":{"code":"18","msg":"Specified image protocol is invalid"}}
...
File 18: cloud1.tri-di.com/scans/D-A000-5d008f27/original_images/img-20.jpg => {"Usage":"0.74234795570374","Resource":"\/file","Error":{"code":"18","msg":"Specified image protocol is invalid"}}
File 19: cloud1.tri-di.com/scans/D-A000-5d008f27/original_images/img-23.jpg => {"Usage":"0.73936891555786","Resource":"\/file","Error":{"code":"18","msg":"Specified image protocol is invalid"}}
I checked the status of the photoscene with this API call:
curl -s https://developer.api.autodesk.com/photo-to-3d/v1/photoscene/KIpe815PrmsCr1Wp73gMFcXQs9wD27P1n1OLiBNfALU/properties
and it returns the following information about my photoscene, showing no files are being attached:
{
"Usage":"0.70800614356995",
"Resource":"\/photoscene\/KIpe815PrmsCr1Wp73gMFcXQs9wD27P1n1OLiBNfALU\/properties",
"next_token":{},
"Photoscenes":{
"Photoscene:{
"photosceneid":"KIpe815PrmsCr1Wp73gMFcXQs9wD27P1n1OLiBNfALU",
"itemName":"KIpe815PrmsCr1Wp73gMFcXQs9wD27P1n1OLiBNfALU",
"clientID":"HAqDtKO7VbuRgH0nL0MFJ0B02ElBEK3l",
"clientStatus":"CREATED",
"type":"all",
"userID":"DtX37KrG1KKBKOHbphoDMzRfn0k=",
"convertStatus":"CREATED",
"projectID":"KIpe815PrmsCr1Wp73gMFcXQs9wD27P1n1OLiBNfALU",
"engineVersion":"3.0.0.3104",
"convertFormat":"rcm,obj,fbx",
"userAgent":"PF_APIv3.2.473-Photofly-WebAPI-FORGE",
"creationDate":"2019-06-26T22:06:23",
"name":"sonautics-D-A000-5d008f27-20190626150712",
"maxResolutionForImage":"100000000",
"UseTitanTextureEngine":"1",
"scenetype":"object",
"status":"PROCESSING",
"Files":{}
}
}
}
Try again and make sure your URL starts with http or https.
Otherwise our server will not treat them as remote resources and proceed to download.

Save a file in a flask-wtf form to MongoDB's GridFS

I'm using the flask-user extension (https://flask-user.readthedocs.io/) along with the mongoengine adapter and flask-wtf to create an HTML form that'll accept several files (images, mp4). The goal is to directly store those files in MongoDB using GridFS but I can't get it to work without storing the image locally on the flask server first...
Here's my non-working code:
#bp.route('/video1', methods=['GET', 'POST'])
#login_required
def video1():
video1Form = Video1Form()
if request.method == 'POST' and video1Form.validate_on_submit():
newVideo1 = Video1(
slogan = video1Form.slogan.data,
objectif = video1Form.objectif.data,
stat1 = video1Form.stat1.data,
stat2 = video1Form.stat2.data
)
name = secure_filename(str(current_user.id) + "__" + "logo.png")
fh = open(video1Form.logo.data, 'rb')
newVideo1.logo.put(fh, filename=name, content_type = "image/jpeg")
newVideo1.save()
return redirect(url_for('video.download'))
if video1Form.errors:
flash(video1Form.errors, 'alert-danger')
return render_template('video/video1.html', video1Form=video1Form)
`
I get the following error on:
[...]File "/home/lucaspierru/Documents/IKADA/sos_sponsors/app/video/routes.py", line 35, in video1
fh = open(video1Form.logo.data, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: 'name_of_the_uploaded_file.ext'
Is it possible to point directly to the file's content and not just get the filename without saving it to the server first ?
I found my mistake:
I forgot to add the enctype="multipart/form-data" attribute to my HTML <form> tag and apparently it is needed to properly upload files, otherwise they'll be considered as text data.

How to solve this warning: failed to open stream: No such file or directory

Recently, I had a problem as:
Warning:include(C:\xampp\htdocs\crackverbal\application\views\errors\html\error_php.php): failed to open stream: No such file or directory in C:\xampp\htdocs\crackverbal\system\core\Exceptions.php on line 269
Warning: include(): Failed opening 'C:\xampp\htdocs\crackverbal\application\views\errors\html\error_php.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\crackverbal\system\core\Exceptions.php on line 269
Warning: include(C:\xampp\htdocs\crackverbal\application\views\errors\html\error_php.php): failed to open stream: No such file or directory in C:\xampp\htdocs\crackverbal\system\core\Exceptions.php on line 269
Warning: include(): Failed opening 'C:\xampp\htdocs\crackverbal\application\views\errors\html\error_php.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\crackverbal\system\core\Exceptions.php on line 269
I used Xampp and codeigniter-3.
I am guessing that
Under application\view\errors\html\ Folder, error_php.php file does not exist Or
Under application\view\ Folder, errors folder does not exist at all.
You can create dummy file and see if there is any error.
Under application/view/, create folder name errors,
then in errors folder, create folder name html
then in html folder, create php file name error_php.php
In error_php.php,
<?php echo "Sample text."; ?>
Just copy error folders in your codeigniter application to codeigniter view folder.
It will correct error page path. And the warnings will removed.
copy "errors" folder from "codeigniter3/application/views" and paste it into your project ("my_project/application/views").
Paste the below code into index.php
$view_folder = '';
// The path to the "views" folder
if ( ! is_dir($view_folder))
{
if ( ! empty($view_folder) && is_dir(APPPATH.$view_folder.DIRECTORY_SEPARATOR))
{
$view_folder = APPPATH.$view_folder;
}
elseif ( ! is_dir(APPPATH.'views'.DIRECTORY_SEPARATOR))
{
header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
echo 'Your view folder path does not appear to be set correctly. Please open the following file and correct this: '.SELF;
exit(3); // EXIT_CONFIG
}
else
{
$view_folder = APPPATH.'views';
}
}
if (($_temp = realpath($view_folder)) !== FALSE)
{
$view_folder = $_temp.DIRECTORY_SEPARATOR;
}
else
{
$view_folder = rtrim($view_folder, '/\\').DIRECTORY_SEPARATOR;
}
define('VIEWPATH', $view_folder);
You should download a fresh codeigniter folder and copy the error folder in the views folder.
Paste it in your own views folder.
please check your folder name may have a space like like: localhost/code igniter/welcome
I just copied "errors" folder from "codeigniter3/application/views" in newly unzipped codeigniter3 folder and paste it into my project ("my_project/application/views"). It solved the problem :)
Create application / view / folder,
in errors folder, create html folder
create php filename error_php.php in the html folder
Check the routs.php file in application>config/routs.php file and change $route['default_controller'] = 'welcome'
to
$route['default_controller'] = 'your default controller name';

Errors in codeigniter-restserver library

I want to use restful in my ci 3.03 application:
I found this tutplus tutorial
I downloaded codeigniter-restserver-master.zip file and copied Format.php and REST_Controller.php(#version 3.0.0) files into /application/libraries/REST directory
I created control application/controllers/api/Users.php :
require_once("application/libraries/REST/REST_Controller.php");
require_once("application/libraries/REST/Format.php");
class Users extends REST_Controller
{
//protected $rest_format = 'json';
function users_get()
{
//$users = $this->user_model->get_all();
$filter_username= $this->get('filter_username');
$filter_user_group= $this->get('filter_user_group');
$filter_active= $this->get('filter_active');
$sort= $this->get('sort');
$sort_direction= $this->get('sort_direction');
//, $filter_user_group, $filter_active, $sort, $sort_direction
$users_list = $this->muser->getUsersList(false, ''/*, $filter_username, $filter_user_group, $filter_active, $sort, $sort_direction, ''*/);
echo '<pre>'.count($users_list).'::$users_lists::'.print_r($users_list,true).'</pre>';
if($users_list)
{
$this->response($users, 200);
}
else
{
$this->response(NULL, 404);
}
}
AND RUNNING URL http://local-ci3.com/api/users I got many errors:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Users::$format
Filename: REST/REST_Controller.php
Line Number: 734
Backtrace:
File: /mnt/diskD_Work/wwwroot/ci3/application/libraries/REST/REST_Controller.php
Line: 734
Function: _error_handler
File: /mnt/diskD_Work/wwwroot/ci3/application/libraries/REST/REST_Controller.php
Line: 649
Function: response
File: /mnt/diskD_Work/wwwroot/ci3/index.php
Line: 292
Function: require_once
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Users::$format
Filename: REST/REST_Controller.php
Line Number: 752
Backtrace:
File: /mnt/diskD_Work/wwwroot/ci3/application/libraries/REST/REST_Controller.php
Line: 752
Function: _error_handler
File: /mnt/diskD_Work/wwwroot/ci3/application/libraries/REST/REST_Controller.php
Line: 649
Function: response
File: /mnt/diskD_Work/wwwroot/ci3/index.php
Line: 292
Function: require_once
Actually I wanted to get some workable library to help me with REST api creation. I think that is preferable way istead of making from zero.
But is this library not workable or does it needs for some fixing? Sorry, what I missed is if this library only for ci 2?
I made search on this forum and found such hint :
I have the same problem when I load both Format.php and
Rest_Controller.php into a controller. After have a quick glance at
Format.php, it appears to be a standalone format conversion helper.
Try to just load Rest_Controller.php and see if your problem goes
away.
I commented line
//require_once("application/libraries/REST/Format.php");
in my controller, but I still get errors like :
Message: Undefined property: Users::$format.
I tried to review code of this library and see that invalid block when data are converted to json format, line 731-757 :
elseif ($data !== NULL)
{
// If the format method exists, call and return the output in that format
if (method_exists($this->format, 'to_' . $this->response->format))
{
// Set the format header
$this->output->set_content_type($this->_supported_formats[$this->response->format], strtolower($this->config->item('charset')));
$output = $this->format->factory($data)->{'to_' . $this->response->format}();
// An array must be parsed as a string, so as not to cause an array to string error
// Json is the most appropriate form for such a datatype
if ($this->response->format === 'array')
{
$output = $this->format->factory($output)->{'to_json'}();
}
}
else
{
// If an array or object, then parse as a json, so as to be a 'string'
if (is_array($data) || is_object($data))
{
$data = $this->format->factory($data)->{'to_json'}();
}
// Format is not supported, so output the raw data as a string
$output = $data;
}
}
If I tried to commented this block, but get error
Message: Array to string conversion
Looks like data are not converted in this case...
Is is possible to fix these errors?
Or can you, please, to tell me advice some codeigniter 3 REST api workable library with similar interface like library above?
Thanks!
I use that lib, work just fine. My suggestion is follow the more relevant installation instruction on github .
you also wrong place the lib file :
Tutorial say :
require(APPPATH'.libraries/REST_Controller.php');
You try :
require_once("application/libraries/REST/REST_Controller.php");
require_once("application/libraries/REST/Format.php");
No need to include the format because on line 407 the lib will load it. And also good to know on line 404 it will load the configuration (application/config/rest.php) it will be your default configuration, and also you can change it to suit your need.
Please let me know if you still got error using my answer :)

Using Soap in magento

I'm trying to follow the information about how to use soap in magento, but always get same message in error.log
If any one experience something similar, that could give me some tip, it will be welcome.
"PHP Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://www.site.com/index.php/api/?wsdl' : failed to load external entity "http://www.site.com/index.php/api/?wsdl"\n in /var/www/test.php on line 1"
$client = new SoapClient('http://www.site.com/api/?wsdl');
$session = $client->login('apiUser', 'apiKey');
$result = $client->call($session, 'somestuff.method');
$result = $client->call($session, 'somestuff.method', 'arg1');
$result = $client->call($session, 'somestuff.method', array('arg1', 'arg2', 'arg3'));
$result = $client->multiCall($session, array(
array('somestuff.method'),
array('somestuff.method', 'arg1'),
array('somestuff.method', array('arg1', 'arg2'))
));
// If you don't need the session anymore
$client->endSession($session);
where you have www.site.com in your SOAP code, replace it with localhost or whatever the correct URL is for your server. You'll also need to replace somestuff.method with real objects and methods as per the Magento documentation