[英]PHP not uploading images to database or server?
在我的網站上有此html表單代碼可以上傳圖像:
<input type="file" name="photo">
也可以使用以下PHP腳本進行處理:
<?php
if (isset($_POST['postnewstory'])){
$newusername = $user_data['username'];
$newemail_address = $user_data['email_address'];
$newnews_title = $_POST['newnews_title'];
$newnews_body = $_POST['newnews_body'];
$newnews_photo=($_FILES['photo']['name']);
$newbutton = $_POST['newbutton'];
$newnews_link = $_POST['newnews_link'];
$newnews_tags = $_POST['newnews_tags'];
//This is the directory where images will be saved
$target = "newsimages/";
$target = $target . basename( $_FILES['photo']['name']);
//update the info in database
mysql_query ("INSERT INTO news (`news_title` ,`news_body` ,`news_photo` ,`news_date` ,`username` ,`news_tags` ,`button`,`news_link`)
VALUES ('$newnews_title', '$newnews_body', '$newnews_photo', now(), '$newusername', '$newnews_tags', '$newbutton', '$newnews_link');") or die (mysql_error());
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target));
echo "<div class='alert alert-success'><strong>This post was sent!</strong></span></div> ";
} else echo "<strong><font color=red>Update did not work, please try again.</font></strong>";
?>
我嘗試了很多更改,但似乎無法讓它上傳照片或照片名稱嗎? 在添加上傳照片代碼之前,僅使用它:
$newnewsphoto = $_POST['photo'];
它可以很好地上傳照片名稱,但是現在我為圖像本身添加了實際的上傳代碼,它似乎不起作用? 表格中的所有其他信息都將被上傳(圖片除外)? 有人對我在這里做錯什么有任何想法嗎? (PS錯誤檢查已退出,做了最后一分鍾)。 謝謝!
您需要在表單html中具有enctype屬性,例如:
<form method="post" action="" enctype="multipart/form-data">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.