图片上传 PHP
Posted
技术标签:
【中文标题】图片上传 PHP【英文标题】:Images Uploading PHP 【发布时间】:2016-10-12 04:45:12 【问题描述】:我一直在做一个比我做起来更容易的项目。
我创建了一个医生和患者之间的完整网站; 我严重卡在上传和检索图片上!!
这是我一直在构建的代码..
<?php session_start();
$un = "xxx";
$pw = "xxx";
$hn = "xxx";
$dbhandle = mysql_connect($hn, $un, $pw) or die("Couldn't connect to the database");
mysql_set_charset('utf8');
$selected = mysql_select_db("dsnnet_login", $dbhandle);
$myusername = $_SESSION['snamed'];
$target_dir = "uploaded/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$sql = mysql_query("UPDATE docTable set photoname='$target_file' WHERE Username= '$myusername'");
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"]))
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false)
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
else
echo "File is not an image.";
$uploadOk = 0;
// Check if file already exists
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000)
echo "Sorry, your file is too large.";
$uploadOk = 0;
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" )
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0)
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
else
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
else
echo "Sorry, there was an error uploading your file.";
header("location: docp.php");
?>
“工作页面”如下:
<?php session_start();
$un = "xxx";
$pw = "xxx";
$hn = "xxx";
$dbhandle = mysql_connect($hn, $un, $pw) or die("Couldn't connect to the database");
mysql_set_charset('utf8');
$selected = mysql_select_db("dsnnet_login", $dbhandle);
$myusername = $_SESSION['snamed'];
$target_dir = "uploaded/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$sql = mysql_query("UPDATE docTable set photoname='$target_file' WHERE Username= '$myusername'");
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"]))
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false)
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
else
echo "File is not an image.";
$uploadOk = 0;
// Check if file already exists
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000)
echo "Sorry, your file is too large.";
$uploadOk = 0;
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" )
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0)
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
else
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
else
echo "Sorry, there was an error uploading your file.";
header("location: docp.php");
?>
谁能帮我弄清楚? 我已经尝试了至少4天了.. 我不会放弃..我愿意学习..
谢谢你们:) 干杯
【问题讨论】:
您在上传时是否遇到任何错误,如提到的那样 - 抱歉,只允许使用 JPG、JPEG、PNG 和 GIF 文件。或抱歉,您的文件太大。 【参考方案1】:在下方查看
1.tag 表单有 enctype="multipart/form-data"
2.如果不是localhost,文件夹上传必须设置permissiontion 777
【讨论】:
【参考方案2】:我可以知道让你卡住的错误信息吗?
在此之前,请检查您的表单 enctype 属性。您的上传表单必须是:
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
【讨论】:
以上是关于图片上传 PHP的主要内容,如果未能解决你的问题,请参考以下文章