我正在尝试创建一个社交网站,希望用户能够上传自己选择的图片。以下代码仅在本地index.php文件中具有本地功能。但我需要链接到另一个页面,以便用户上传图像。 Godaddy的支持者说我应该能够做到,但是现在到第二天,我正在寻求帮助。
代码如下:
if(isset($_FILES["fileUpload"]["name"])) {
$imageFile = ($_FILES["fileUpload"]["name"]);
$imageType = ($_FILES["fileUpload"]["type"]);
$validext = array("jpeg", "jpg", "png");
$fileExt = pathinfo($imageFile, PATHINFO_EXTENSION );
echo $fileExt . "<br>";
$ready = false;
if((($imageType == "image/jpeg") || ($imageType == "image/jpg") ||
($imageType == "image/png")) && in_array($fileExt, $validext)) {
echo "was valid image<br>";
}else{
echo "was not a valid image<br>";
$ready = false;
}
if($_FILES["fileUpload"]["size"] < 500000) {
$ready = true;
echo "file size is " . $_FILES["fileUpload"]["size"] . "<br>";
}else{
echo "file was TO BIG!<br>";
echo "file size is " . $_FILES["fileUpload"]["size"] . "<br>";
$ready = false;
}
if($_FILES["fileUpload"]["error"]) {
echo "looks like there was an error: " . $_FILES["fileUpload"]["error"] . "<br>";
$ready = false;
}
$targetPath = "images/".$imageFile;
$sourcePath = $_FILES["fileUpload"]["tmp_name"];
if(file_exists("images/".$imageFile)) {
echo "File already there <br>";
$ready = false;
}
if($ready == true) {
move_uploaded_file($sourcePath, $targetPath);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Upload </title>
</head>
<body>
<form action="index.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileUpload" id="fileUpload">
<input type="submit" value="Upload file">
</form>
</body>
</html>
这很标准。我不知道问题出在Godaddy还是代码上。我根本不是PHP专家。
任何建议将不胜感激。再说一次:如果在根站点级别将其命名为index.php,则此方法有效,但是如果我将其命名为其他名称或将其移至其他位置,则它将不起作用,并且将index.php放入其中非常容易。
先谢谢了。一定有人遇到过这个...