2011-08-22 59 views
0

正在使用共享服務器。使用捲髮時將路徑和文件信息保存到mysql

我可以使用捲曲保存外部圖像。但我無法將路徑和文件信息保存到mysql。

<form method="post" enctype="multipart/form-data"> 
<table> 
<tr><td><b>URL: </b> </td><td><input type="**text**" name="url" id="url" size=40></td> </tr> 
<tr><th colspan=2><p><input name="upload" type="submit" class="box" id="upload" value="Upload"></p></th></tr> 
</table> 
</form> 

<?php 

set_time_limit(0); 
ini_set('display_errors',true);//Just in case we get some errors, let us know.... 

if (isset($_POST['upload'])) 
{ 

    $url = $_POST['url']; 
    $dir = "./files/"; 

    $fileName = $_FILES['url']['name']; 
    $fileSize = $_FILES['url']['size']; 
    $fileType = $_FILES['url']['type']; 
    $filePath = $dir . $fileName; 

    $ch = curl_init($url); 
    $fp = fopen ($local_file, 'w+'); 
    $ch = curl_init($remote_file); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 50); 
    curl_setopt($ch, CURLOPT_FILE, $fp); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_ENCODING, ""); 
    curl_exec($ch); 
    curl_close($ch); 

    include ('db.php'); 
    if(!get_magic_quotes_gpc()) 
    { 
    $fileName = addslashes($fileName); 
    $filePath = addslashes($filePath); 
    } 
    $query = "INSERT INTO upload2 (name, size, type, path) ". "VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')"; 
mysql_query($query); 
mysql_close($conn); 
} 

當我改變輸入類型,從文件到文本這是行不通的。

我需要得到的文件信息,並保存到MySQL服務器

+0

我想您已經刪除了一大堆這裏捲曲代碼?就這樣,我很清楚,你想要的是從用戶的計算機上傳文件,而不是**通過cURL從URL中獲取文件?或者你想選擇做嗎? – DaveRandom

+0

Yaa curl評論在此處添加代碼時被刪除,現在添加了他們。我希望創建一個將url作爲輸入並將文件保存到服務器的表單。像http://example.com/example.jpg – ajk

+0

對於這樣簡單的事情,我建議你使用[file_get_contents()](http://www.php.net/manual/en/function.file-get -contents.php)而不是用cURL搞亂。雖然cURL可以更精確地控制獲取文件的請求,但這不是您需要的,如果您需要添加一些標題或(出於一些奇怪的原因)POST來獲取文件,您可以使用流來實現上下文。 – DaveRandom

回答

0

有此一展身手:

<form method="post" enctype="application/x-www-form-urlencoded"> 
    <table> 
    <tr> 
     <td style="font-weight:bold;">URL:</td> 
     <td><input type="text" name="url" id="url" size=40 value="http://" /></td> 
    </tr> 
    <tr> 
     <!-- Why is this a <th> and not a <td> ? --> 
     <th colspan="2"><input name="upload" type="submit" class="box" id="upload" value="Upload" /></th> 
    </tr> 
    </table> 
</form> 

<?php 

    set_time_limit(0); 
    ini_set('display_errors',true);//Just in case we get some errors, let us know.... 

    if (isset($_POST['upload'])) { 

    // Build local file system paths 
    $baseDir = "./files"; 
    $fileName = basename(rtrim($_POST['url'],'/')); // This means that if someone specifies the root of a domain (/) it will call the local file "domain" 
    $filePath = rtrim($baseDir,'/')."/$fileName"; 

    // Try to parse the URL we have been given and add any missing required info 
    if ((!$parts = parse_url($_POST['url'])) || !isset($parts['host'])) die('ERROR: The specified URL is invalid'); 
    $url = (isset($parts['scheme']) && $parts['scheme']) ? $parts['scheme'].'://' : 'http://'; 
    if (isset($parts['user']) && $parts['user']) { 
     $url .= $parts['user']; 
     if (isset($parts['pass']) && $parts['pass']) $url .= ':'.$parts['pass']; 
     $url .= '@'; 
    } 
    $url .= $parts['host']; 
    if (isset($parts['port']) && $parts['port']) $url .= ':'.$parts['port']; 
    $url .= (isset($parts['path']) && $parts['path']) ? $parts['path'] : '/'; 
    if (isset($parts['query']) && $parts['query']) $url .= '?'.$parts['query']; 
    if (isset($parts['fragment']) && $parts['fragment']) $url .= '#'.$parts['fragment']; 

    print("Fetching file '$url' to '$filePath'<br />\n"); 

    // Open a local file point in write mode 
    if (file_exists($filePath)) die("ERROR: The file '$filePath' already exists"); 
    $fp = fopen($filePath,'w') or die("ERROR: Could not open local file '$filePath' for writing!"); 

    // Let's go cURLing... 
    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 50); 
    curl_setopt($ch, CURLOPT_FILE, $fp); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_ENCODING, ""); 
    curl_exec($ch); 
    $fileSize = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD); 
    $fileType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); 
    $transferTime = curl_getinfo($ch, CURLINFO_TOTAL_TIME); 

    print("cURL operation complete (took $transferTime seconds)<br />\n"); 

    // Close cURL and the local file pointer, we're done with them both 
    curl_close($ch); 
    fclose($fp); 

    // Check the $fileSize reported by cURL is the same as the file size on disk 
    if ($fileSize != ($fs = filesize($filePath))) { 
     print("WARNING: file size reported by cURL ($fileSize bytes) does not match size of file on disk ($fs bytes)<br />\nUsing size of file on disk for DB insert<br />\n"); 
     $fileSize = $fs; 
    } 

    include ('db.php'); 
    $query = "INSERT INTO upload2 (name, size, type, path) ". 
    "VALUES ('".mysql_real_escape_string($fileName)."', '$fileSize', '$fileType', '".mysql_real_escape_string($filePath)."')"; 

    print("Running MySQL Query: $query<br />\n"); 

    mysql_query($query); 
    mysql_close($conn); 

    print('Done!'); 

    } 

?> 
+0

添加了幾個額外的位間距,使其更具可讀性,並且額外的評論 – DaveRandom

+0

thanx很多,現在要測試 – ajk

+0

工程很好,thanx再一次 – ajk

相關問題