2016-11-14 157 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

相关问题