2016-11-14 158 views
0

我剛開始在這2個最後幾周學習PHP,我試圖編寫一個表單發送信息,包括圖像到數據庫。 我的問題是:我想保存圖像在一個特定的文件夾,並只保存其數據庫上的URL(或甚至它的名字,但它必須是與我插入圖像信息相關的東西)。我設法讓所有其他領域的工作期望的形象,因爲我無法弄清楚如何使其工作。 所以我需要幫助才能使它工作。謝謝大家,祝你有美好的一天,我會在這裏與你分享我的代碼。在數據庫中存儲圖像的URL(PHP/MySql)

<?php 

if(isset($_POST['regie']))  $regie=$_POST['regie']; 
else  $regie=""; 

if(isset($_POST['annonceur']))  $annonceur=$_POST['annonceur']; 
else  $annonceur=""; 

if(isset($_POST['categorie']))  $categorie=$_POST['categorie']; 
else  $categorie=""; 

if(isset($_POST['titre']))  $titre=$_POST['titre']; 
else  $titre=""; 

if(isset($_POST['image']))  $image=$_POST['image']; 
else  $image=""; 

if(isset($_POST['nombrepassage']))  $nombrepassage=$_POST['nombrepassage']; 
else  $nombrepassage=""; 

if(isset($_POST['datefin']))  $datefin=$_POST['datefin']; 
else  $datefin=""; 


if(empty($regie) OR empty($annonceur) OR empty($categorie) OR empty($titre) OR empty($image) OR empty($nombrepassage) OR empty($datefin)) 
    { 
    echo '<font color="red">Attention, aucun champs ne peut rester vide !</font>'; 
    } 


else  
    { 

$db = mysql_connect('localhost', 'root', 'test123') or die('Erreur de connexion '.mysql_error()); 


    mysql_select_db('geomedia',$db) or die('Erreur de selection '.mysql_error()); 


    $sql = "INSERT INTO ajout(Régie, Annonceur, Catégorie, Titre, Lien, Image, Nombre_Passage, Date_Fin) VALUES('$regie','$annonceur','$categorie','$titre','$image','$nombrepassage','$datefin')"; 


    mysql_query($sql) or die('Erreur SQL !'.$sql.'<br>'.mysql_error()); 


    echo 'Vos infos ont bien été ajoutées.'; 

    mysql_close(); 
    } 
?> 

+0

您不必代碼上傳圖像,請參閱http://stackoverflow.com/questions/35253550/upload-a-file-using-php – Garf365

+0

我有一個我試圖包含在我的文件中,但它沒有工作,謝謝這個工作正常。 – SakiManiac

回答

0

您必須使用,一個<input type="file">。 您的形式應該是這樣的:

<form enctype="multipart/form-data" action="#" method="post"> 
    <input type="hidden" name="MAX_FILE_SIZE" value="10000000" /> 
    Select file : <input name="my-file" type="file" /> 
    <input type="submit" name="send-file" /> 
</form> 

然後,你將不會被$ _ POST訪問該文件,但是$ _FILES: http://php.net/manual/en/features.file-upload.post-method.php

文檔:

$_FILES['userfile']['name'] 

的原始的名字文件在客戶機上。

$_FILES['userfile']['type'] 

該文件的MIME類型,如果瀏覽器提供了該信息。一個例子就是「image/gif」。然而,這種MIME類型在PHP方面沒有被檢查,因此不會將其值視爲理所當然。

$_FILES['userfile']['size'] 

上傳文件的大小(以字節爲單位)。

$_FILES['userfile']['tmp_name'] 

上傳文件存儲在服務器上的臨時文件名。

$_FILES['userfile']['error'] 

與此文件上傳相關的錯誤代碼。

此外,您在文檔中有許多示例。

由於存在許多安全隱患,因此應該仔細設計PHP上傳文件。 與應對的一個例子:https://www.tutorialspoint.com/php/php_file_uploading.htm

相關問題