Jump to content

error "Unable to copy" adding images from API


darrenc
 Share

Recommended Posts

site setup:

  • /public_html - regular site files, unrelated to pw
    • /dev - processwire lives here (subdomain pointed dev.domain.com)
      • /test - where my bootstrapped script runs
        • /images - images to be imported

code in /test:

// processwire bootstrap
include '../index.php'; 
$pages = wire("pages");
$sanitizer = new Sanitizer();

foreach ($csv as $row) {
	// test if the page exists
	$name = $sanitizer->pageName( $row["title"], true );
	$p = $pages->get("name={$name}");
	if (!$p->id) {
		// if not, create one
		$p = new Page();
	}

	$p->template = "sampler";
	$p->parent = $pages->get("/samplers/");

	$p->name = $name; 
	$p->title = $sanitizer->text( $row["title"] );

	// do a bunch more stuff
	
	$p->save();
	echo "saved   id:{$p->id}   {$p->url}\n";
	
	// add images, y u no store?
	$p->images->add( "./images/" . rawurlencode($row["image"]) );
}

error:

Error: 	Uncaught exception 'WireException' with message 'Unable to copy: ./images/Best%20MCC%20Pic-2.jpg => /home/samplers/public_html/dev/site/assets/files/1027/best_20mcc_20pic-2.jpg' in /home/samplers/public_html/dev/wire/core/Pagefile.php:109

Stack trace:

#0 /home/samplers/public_html/dev/wire/core/Wire.php(271): Pagefile->___install('./images/Best%2...')

#1 /home/samplers/public_html/dev/wire/core/Wire.php(229): Wire->runHooks(Array, Array)

#2 /home/samplers/public_html/dev/wire/core/Pagefile.php(74): Wire->__call('install', Array)

#3 /home/samplers/public_html/dev/wire/core/Pagefile.php(74): Pageimage->install('install', Array)

#4 /home/samplers/public_html/dev/wire/core/Pagefile.php(50): Pagefile->setFilename('./images/Best%2...')

#5 /home/samplers/public_html/dev/wire/core/Pageimage.php(68): Pagefile->__construct('./images/Best%2...')

#6 /home/samplers/public_html/dev/wire/core/Pageimages.php(38): Pageimage->__construct(Object(Pageimages), './images/Best%2...')

#7 /home/samplers/public_html/dev/test/index.php (line 109 of /home/samplers/public_html/dev/wire/core/Pagefile.php) 

This error message was shown because you are logged in as a Superuser. Error has been logged.

Probably relevant:

$row["image"] spits out a regular "Image name here 9000.jpg" string.

It's the end of the day and I'm a bit bleary-eyed, but I feel as though I've tried every path/url combination and sanitization of the image name string. I haven't figured whether it is just me doing something wrong (likely) or some server configuration.

Can anyone tip me off as to what's going wrong? 

Link to comment
Share on other sites

adrian, I'm pretty sure I tried all combinations of the plain string first, but I could be mistaken. I'll be going back through this today and, this time, systematically keeping track of the attempts which failed. 

try using the full path to the image

At risk of asking stupid questions, which path is "the full path" ? The web hosting account file path? the public_html root relative path? the sub.domain.com root-relative path? $config->urls->root? $config->paths->root? is this an issue with bootstrapping that I have to treat differently? It's all relative one way or another unless i'm stating http://dev.domain.com/test/images/ correct? 

Those were all the questions swirling in my mind yesterday. I'm sure I'm doing something simple/obvious wrong, I just couldn't figure out which.

Hopefully with fresh eyes I'll figure it out today.

Link to comment
Share on other sites

Exactly as you said it, thanks adrian. The fault was entirely my own -- when I couldn't get the relative path working I jumped to the conclusion that the spaces in the filename were causing issues. That, of course, was a dead end.

final working code for anyone interested:

// do a bunch more stuff
	
$p->save();
echo "saved   id:{$p->id}   {$p->url}\n";

// add images, y u no store? 
// Answer: using relative path was wrong (and rawurlencode is unnecessary)
// $p->images->add( "./images/" . rawurlencode($row["image"]) );

// get the full server path to processwire, and from there the images
$path = $config->paths->root . "test/images/" . $row["image"]; 

$p->images->add( $path );
$p->save();

Thanks again, Adrian.

  • Like 3
Link to comment
Share on other sites

  • 1 year later...

Hi guys,

I've got a similar issue.

I have a script which is pulling in properties and images from an external XML file.

The script fails when I try to add the image via api with the following error:

Task "httpdocs/import-properties-sale-xml.php" completed with error in 65 seconds, output:

Adding new page: mickleton-road--earlsdon--coventry

Error: Uncaught exception 'WireException' with message 'Unable to copy: http://www.dezr...')
#1 [internal function]: Pageimage->___install('http://www.dezr...')
#2 /var/www/vhosts/brianholt.co.uk/httpdocs/wire/core/Wire.php(398): call_user_func_array(Array, Array)
#3 /var/www/vhosts/brianholt.co.uk/httpdocs/wire/core/Wire.php(333): Wire->runHooks('install', Array)
#4 /var/www/vhosts/brianholt.co.uk/httpdocs/wire/core/Pagefile.php(83): Wire->__call('install', Array)
#5 /var/www/vhosts/brianholt.co.uk/httpdocs/wire/core/Pagefile.php(83): Pageimage->install('http (line 117 of /var/www/vhosts/brianholt.co.uk/httpdocs/wire/core/Pagefile.php)

This error message was shown because you are using the command line API. Error has been logged.

What is strange is that this script works fine on one server that I developed the website on but not the live server it is now on.

The image URL looks as follows: http://www.dezrez.com/estate-agent-software/ImageResizeHandler.do?PropertyID=3878781&photoID=3&AgentID=259&BranchID=600&width=800.

If you directly access that URL the .do file processes the image and redirects to the image URL.

Could this be the problem? Like I say it all worked fine on the other server.

Many Thanks for any help!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...