Hello, I need a PHP code that can get the image url from "$_POST['big_img']"(eg: http://i.dpstatic.com/misc/dps_logo.png) and more it to my server, and rename the image name to a random (eg: rand().dps.logo.png). The directory is /home/sharing/sub-domains/tuts/public_html/images/big/
I don't see why CURL should be a must. Here is my solution to your problem : <?php $path = '/home/sharing/sub-domains/tuts/public_html/images/big/'; if(isset($_POST['big_img'])) { $image = file_get_contents(urldecode($_POST['big_img'])); if($image !== false) { if(strpos($_POST['big_img'],"/") !== false) { $name = explode("/",$_POST['big_img']); $total = count($name); $handle = fopen($path.rand()."_".$name[$total-1],"w") or die("Could not create : ".$path.rand()."_".$name[$total-1]); if($handle !== false) { fwrite($handle,$image); fclose($handle); echo 'The file has been successfully saved !'; } } } else { echo 'Wrong path !'; } } ?> PHP:
<?php $path = '/home/sharing/sub-domains/tuts/public_html/images/big/'; if(isset($_POST['big_img'])) { //$image = file_get_contents(urldecode($_POST['big_img'])); $curl_handle=curl_init(urldecode($_POST['big_img'])); curl_setopt($curl_handle, CURLOPT_NOBODY, true); $result = curl_exec($curl_handle); $retcode = false; if($result !== false) { $status = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE); if($status == 200) $retcode = true; } curl_close($curl_handle); if($retcode) { $curl_handle=curl_init(); curl_setopt($curl_handle,CURLOPT_URL,urldecode($_POST['big_img'])); curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2); curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1); $image = curl_exec($curl_handle); curl_close($curl_handle); if($image !== false) { if(strpos($_POST['big_img'],"/") !== false) { $name = explode("/",$_POST['big_img']); $total = count($name); $handle = fopen($path.rand()."_".$name[$total-1],"w") or die("Could not create : ".$path.rand()."_".$name[$total-1]); if($handle !== false) { fwrite($handle,$image); fclose($handle); echo 'The file has been successfully saved !'; } } } } else { echo 'File not found !'; } } ?> PHP:
Hi, I want this same functionality on submit of text box: The image url will be input in this text box How do I achieve the same? <tr valign="top"> <th scope="row"><label><?php _e( 'External Image', 'testify' ) ?></label></th> <td> <input type="text" name="testify_meta[ext_image]" id="ext_image" value="<?php if( $project && !empty($meta['ext_image']) ) echo $meta['ext_image']; ?>" /> </td> </tr> <div class="testify-button"><input type="submit" name="save_project" value="<?php _e( 'Save Project', 'testify' ) ?>"></div> <input type="hidden" name="project_id" value="<?php if ($project) echo $project->ID ?>"> </form> </div>