上传文件,将文件路径插入MySQL并返回上传页面,其中包含URL中的字符串

时间:2014-04-25 06:56:20

标签: php mysql file-upload

这些天我正在设计一个文件上传系统。 我想将图像上传到服务器并将文件路径存储在MySQL数据库中。 保存文件路径后,用户将被重定向回上传页面,其中包含URL中的用户字符串uid=1

upload.php的

<form action="<?php echo "upload_file.php?id=" . $cid ; ?>" method="post" enctype="multipart/form-data" name="photo" id="photo">
        <table width="295" border="0" align="center">
          <tr>
            <td width="289">Photos</td>
          </tr>
          <tr>
            <td class="formdisplay"><label for="priestname"></label>
              <label for="photo"></label>
            <input type="file" name="file" id="file" /></td>

          </tr>
          <tr>
            <td class="formdisplay"><input type="submit" name="submit" id="submit" value="Submit" /></td>
          </tr>
        </table>
      </form>

$cid是一个使用GET [upload.php?id=1]

从URL检索用户ID的变量

并帮我创建 upload_file.php 文件。

2 个答案:

答案 0 :(得分:2)

首先form需要enctype="multipart/form-data" 现在,在您的情况下,我将创建另一个hidden输入,而不是在操作中使用变量。

以下是我正在使用的示例

//use this to get the extension of the file
function findexts ($filename) 
 { 
     $filename = strtolower($filename) ; 
     $exts = split("[/\\.]", $filename) ; 
     $n = count($exts)-1; 
     $exts = $exts[$n]; 
     return $exts; 
 } 
    $ext = findexts ($_FILES['photo']['name']) ;
    //create files name
    $player_id2 = $player_id.".";
    $imgforDB = $player_id2.$ext;
    //the directory which you want to store the file
    $target = "img/players/";
    $target = $target . $player_id2.$ext;
    if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
        {           
            //Tells you if its all ok
            echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory";
            $sql2 = $mysqli->query("UPDATE  **** SET `player_img`='$imgforDB' WHERE ***'")
            or die(mysqli_error($mysqli));
            }
            else {
            //Gives and error if its not
            echo "Sorry, there was a problem uploading your file.";
     }

这几乎就是整个想法。在数据库上,它将存储为player12.jpg

答案 1 :(得分:1)

我希望我的样本会有所帮助:

<? 
ob_start();
$uploaddir = 'path_to_file/';
$uploadfile = $uploaddir . basename($_FILES['file']['name']);
echo $uploadfile;
if (!move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
    echo "Upload error.\n";
} 

//Query to store path for eample with PDO
$q = $conn_xyz->prepare("insert into images (path) Values ('".$uploadfile."')");
$q->execute();
//Store $cid in hidden form element then it is post, else it will be GET
header("Location: upload_form.php?cid=".$_GET["cid"]);
?>

我们的代码中有一些错误:

<form action="<?php echo "upload_file.php?id=" . $cid ; ?>" method="post" enctype="multipart/form-data" name="photo" id="photo">
            <table width="295" border="0" align="center">
              <tr>
                <td width="289">Photos</td>
              </tr>
              <tr>
                <td class="formdisplay"><label for="priestname"></label>
                  <label for="photo"></label>
                <input type="file" name="file" id="file" /></td>

              </tr>
              <tr>
                <td class="formdisplay"><input type="submit" name="submit" id="submit" value="Submit" /></td>
              </tr>
            </table>
          </form>

好的代码是:

<form action="<?php echo "process.php?cid=".$cid.""; ?>" method="post" enctype="multipart/form-data" name="photo" id="photo">
        <table width="295" border="0" align="center">
          <tr>
            <td width="289">Photos</td>
          </tr>
          <tr>
            <td class="formdisplay"><label for="priestname"></label>
              <label for="photo"></label>
            <input type="file" name="file" id="file" /></td>

          </tr>
          <tr>
            <td class="formdisplay"><input type="submit" name="submit" id="submit" value="Submit" /></td>
          </tr>
        </table>
      </form>

请参阅您编写的表格的第一行id = not cid =并且缺少一些引号。