简体   繁体   English

curl和php问题—空白页

[英]curl and php problem — blank page

I am attempting to download images saved in a url to a local folder, and have made the following attempt at doing so with curl. 我正在尝试将保存在url中的图像下载到本地文件夹,并尝试进行以下卷曲操作。 I would like to know if it is necessary to include curl or download it seperateley, or if my function will just work. 我想知道是否有必要包含curl或单独下载它,或者我的功能是否能正常工作。 I would like to know if there are any obvious problems with my implementation below. 我想知道下面的实现是否存在任何明显的问题。 I am aware of the sql vulnerability and I am switching to prepared statements. 我知道sql漏洞,现在我切换到准备好的语句。 I have trimmed non relevant parts of the code for brevity. 为了简洁起见,我已经修剪了代码中不相关的部分。

edit: the function is out of the while loop. 编辑:该函数不在while循环中。 The page displays if I comment out the call the the function, otherwise I only get a blank page. 如果我将调用函数注释掉,则会显示该页面,否则,我只会得到一个空白页面。 Why is this 为什么是这样

<?php
header("Content-Type: text/html; charset=utf-8");
if (isset($_GET["cmd"]))

  $cmd = $_GET["cmd"];

else

  die("You should have a 'cmd' parameter in your URL");

 $pk = $_GET["pk"];

$con = mysql_connect("localhost","someuser","notreal");

if(!$con)

{

die('Connection failed because of' .mysql_error());

}
mysql_query('SET NAMES utf8'); 

mysql_select_db("somedb",$con);

if($cmd=="GetAuctionData")

{

$sql="SELECT * FROM AUCTIONS WHERE ARTICLE_NO ='$pk'";

$sql2="SELECT ARTICLE_DESC FROM AUCTIONS WHERE ARTICLE_NO ='$pk'";

$htmlset = mysql_query($sql2);

$row2 = mysql_fetch_array($htmlset);



$result = mysql_query($sql);

function savePicture($imageUrl) {
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $lastImg);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
    $fileContents = curl_exec($ch);
    curl_close($ch);
    $newImg = imagecreatefromstring($fileContents);
    return imagejpeg($newImg, "./{$pk}.jpg",100);
}

while ($row = mysql_fetch_array($result))
{



$lastImg = $row['PIC_URL']; 


savePicture($lastImg);


<div id='rightlayer'>

<img src='./".$pk.".jpg' width='".$outputWidth."' height='".$outputHeight."'>


</div>

</div>

</div>";



}



}

mysql_free_result($result);

You'll get an error if you declare a function inside a loop when the loop is run more than one time. 如果在循环中多次运行时在循环内声明一个函数,则会出现错误。 So you should declare the savePicture function outside while . 所以,你应该声明savePicture之外的功能while

I'd take the function definition out of the while block. 我将从while块中取出函数定义。

In my opinion your using curl for the sake of using curl here, a simpler method would be to use file get contents . 我认为您为了在这里使用curl而使用curl,一种更简单的方法是使用file get contents

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM