繁体   English   中英

在数据库中存储图像的URL(PHP / MySql)

[英]Storing an image's URL in database (PHP / MySql)

在过去的这两个星期中,我才刚刚开始学习PHP,我正在尝试编写一种将信息(包括图像)发送到数据库的表单。 我的问题是:我想将图像保存在特定文件夹中,并且仅将其URL保存在数据库中(甚至是其名称,但必须与我随图像插入的信息有关)。 我设法使所有其他字段都能正常工作,因为我不知道如何使它工作。 因此,我需要帮助才能使其正常工作。 谢谢大家,祝您有愉快的一天,我将在这里与您分享代码。

<?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();  
    }  
?>

为此,必须使用<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”。 但是,不会在PHP端检查此mime类型,因此不要将其值视为理所当然。

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

上载文件的大小(以字节为单位)。

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

上载文件存储在服务器上的文件的临时文件名。

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

与此文件上传相关的错误代码。

此外,您在文档中有很多示例。

由于存在许多安全隐患,因此应谨慎设计PHP中的文件上传。 解决这些问题的示例: https : //www.tutorialspoint.com/php/php_file_uploading.htm

暂无
暂无

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

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