我正在为我的社交网络使用照片上传脚本而且它不会工作,这里是代码:
upload.php的
<style>
.sucess{
color:#088A08;
}
.error{
color:red;
}
</style>
<?php
$file_exts = array("jpg", "bmp", "jpeg", "gif", "png");
$upload_exts = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($upload_exts, $file_exts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
// Enter your path to upload file here
if (file_exists("profile_pictures/" .
$_FILES["file"]["name"]))
{
echo "<div class='error'>"."(".$_FILES["file"]["name"].")".
" already exists. "."</div>";
}
else
{
$my_id = $_SESSION['user_id'];
$new_file_name = $my_id;
move_uploaded_file($_FILES["file"]["tmp_name"],
"profile_pictures/" . $new_file_name);
echo "<div class='sucess'>"."Stored in: " .
"profile_pictures/" . $new_file_name."</div>";
}
}
}
else
{
echo "<div class='error'>Invalid file</div>";
}
?>
change_photo.php
<html>
<body>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
它只是说:
上传:A FILE.png类型:image / png文件大小:1028.0830078125 kB临时文件: C:\ XAMPP \ TMP \ phpD21D.tmp
警告:move_uploaded_file():copy()函数的第二个参数 不能是C:\ xampp \ htdocs \ Social Network \ upload.php上的目录 第43行
警告:move_uploaded_file():无法移动 &#39; C:\ XAMPP \ TMP \ phpD21D.tmp&#39;到&#39; profile_pictures /&#39;在 第43行的C:\ xampp \ htdocs \ Social Network \ upload.php存储于: profile_pictures /
答案 0 :(得分:1)
您的$my_id
变量似乎是空的。
您是否在脚本的最顶部使用了session_start();
?