Recently, we had a client who needed to upload hundreds of records at a time to his site via a csv file. These records would have 12 columns each. Some columns would come directly from the columns in the csv file while others had to be derived. Not that complicated really, things like google lat/lon for example would be derived from the, address, city, country, etc columns.
To The point
These records, for some reason we won’t discuss here, has an image url as one of its columns. This brings up a fairly simple situation, or so i thought. We needed the following:
- Download the file to server
- Upload to wordpress
- attach to wordpress media gallery
- get local image url and attach it to record for saving
Now this should be incredibly easy when you consider how versatile wordpress is. Unfortunately, we found out quite quickly that the wordpress function wp_insert_attachment($attachment, $filename, $parent_post_id ) isn’t a one liner function. Actually its more like the following
$attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/.[^.]+$/', '', basename($filename)), 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment( $attachment, $filename, 37 );
More importantly, the $filename variable “MUST be on the uploads directory.” according to the function reference.
The Search Was On
So what did we do? The same thing we do every day, run to google. Turns out, no one could give me a straight forward
answer regarding how to fix this. Not even those shady little forums … you know those you have never heard of before, never use, but hey if they have the info…. you know, kinda like this one…. (: -)~
Well, we here at Beyond Programs like to go brute force when things get tough. Make a curl script. Yup! How simple was that…. UMM, unfortunately not easy enough. Maybe you out there on the world wide intertubes would have better luck than I BUT… Oh the BUT
Turns out, for me at least, that getting the image via curl returns the files contents. What am i going to do with this other than stick it into a file? You know
if($out = fopen($local_file, ‘c’))
fwrite($out, trim($content));
Again another wall. Since i’m, knee deep in a plugin, i’m surrounded by a place where the header has long been launched.
Technicalities
Lets take a closer look at the patient. Since the headers have long been launched, i cant use this nifty trick i learnt where i simply create an image header. Yeah you know it, image header, stuff file with funky code, save file and voila.
header(‘Content-Type: image/jpeg’);
header(‘Content-Disposition: attachment; filename=’ . urlencode($filename));
Well nice little write up for nothing, seeing that again, i’m knee deep in a plugin so that’s not happening. Did i mention that we here at Beyond Programs like to go brute force when the stupid thing won’t work?
Remember earlier I mentioned creating a curl script? Well we did, tested it, and all that stuff i mentions actually works if you aren’t knee deep in wordpress. So for those of you as desperate as I for a solution, and ready to bend over backwards for a solution, i have something.
SHHHHhhhhhh
Curl to the curl script. Yeah I said it. Curl knee deep in wordpress to another place on your site (login protected ofcourse) and have that script do all the image saving etc. Boooyaaaa, i said brute force and um, may I add, it works flawlessly.
Send whatever you want to the script. Most importantly, send the file it needs to go get.
$url = … “?local_file=”.trim($local_file).”&remote_file=”.$remote_url;
$ch = curl_init(trim($check_url));
curl_exec($ch);
Your standalone script should handle the entire fetching and saving process. Your main script will finish the attachment to the wordpress gallery then get the url for the record column.
What about
What about file_get_contents you say? That would cut half this trash out. Well did I add that some of the links in the csv wern’t real? They FORWARDED to somewhere else. Also, i don’t believe it was working even without this issue, you try and let me know. Ultimately, when the stupid code stops working we get to Brute Forcing.
Conclusion
If someone asks you to get an image from some other domain and upload it to theirs, run. Bad joke maybe, but in my case, the images were all part of an advertising and listings agreement. A major network was supplying the info and the customer didn’t want to go the api route.
If you have a legit reason for fetching images and uploading them to your server/wordpress then its possible. You must hack at it but it’s possible.
I’ll have to add some actual files to this at some point seeing that it takes several lines to do all of this. However, for those of you really going down such a road, with whats explained here, you already have the code. Ok, enough jedi, at least a few lines
……………………………………………………………………………………………………………………
Main File
$remote_url = addslashes(strip_tags($arr[11]));
$raw_file = explode(“/”,$remote_url);
$filename = $uploads[‘path’].”/”.$raw_file[sizeof($raw_file)-1];//get filename
if(http_get_file($remote_url, $filename)){//if succesfully downloaded then attach to gallery
……………………………………………………………………………………………………………………
Curl to curl
function http_get_file($remote_url, $local_file) {
$check_url = “whatever.php?local_file=”.trim($local_file).”&remote_file=”.$remote_url;
$ch = curl_init(trim($check_url));
curl_exec($ch);
……………………………………………………………………………………………………………………
Standalone
$local_file = $_GET[“local_file”];
$remote_file = $_GET[“remote_file”];
$raw_file = explode(“/”,$remote_file);
$filename = $raw_file[sizeof($raw_file)-1];
header(‘Content-Type: image/jpeg’);
header(‘Content-Disposition: attachment; filename=’ . urlencode($filename));
require(“../../../wp-load.php”);
if (is_admin()) {
- Descargue el archivo en el servidor
- Subir a wordpress
- adjuntar a la Galería de medios de wordpress
- obtener url de imagen local y adjuntarlo a grabar para guardar
$attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/.[^.]+$/', '', basename($filename)), 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment( $attachment, $filename, 37 );
……………………………………………………………………………………………………………………
Main File
$remote_url = addslashes(strip_tags($arr[11]));
$raw_file = explode(“/”,$remote_url);
$filename = $uploads[‘path’].”/”.$raw_file[sizeof($raw_file)-1];//get filename
if(http_get_file($remote_url, $filename)){//if succesfully downloaded then attach to gallery
……………………………………………………………………………………………………………………
Curl to curl
function http_get_file($remote_url, $local_file) {
$check_url = “whatever.php?local_file=”.trim($local_file).”&remote_file=”.$remote_url;
$ch = curl_init(trim($check_url));
curl_exec($ch);
……………………………………………………………………………………………………………………
Standalone
$local_file = $_GET[“local_file”];
$remote_file = $_GET[“remote_file”];
$raw_file = explode(“/”,$remote_file);
$filename = $raw_file[sizeof($raw_file)-1];
header(‘Content-Type: image/jpeg’);
header(‘Content-Disposition: attachment; filename=’ . urlencode($filename));
require(“../../../wp-load.php”);
if (is_admin()) {