php中move_uploaded_file()函数的问题
我在 apache localhost 中运行了代码,并在我的主机中进行了尝试。在这两个方法中,该方法移动了文件,但文件看起来为 0kb。
这是代码:
if(isset($_POST['upload'])){
if($_FILES['profile_foto']['size']>0&&$_FILES['profile_foto']['size']<102400){
$image_extension=explode("/",$_FILES['profile_foto']['type']);
if($image_extension[1]!="jpeg")
echo "<script type='text/javascript'>alert('The extension of the profile image must be jpeg!!!')</script>";
else{
if($_POST['image_id']==""){
$image_id= md5(uniqid());
$_FILES['profile_foto']['name']="tempo/".$image_id.".jpg";
move_uploaded_file($_FILES['profile_foto']['temp_name'],$_FILES['profile_foto']['name']);
list($width,$height)=getimagesize("tempo/".$image_id.".jpg");
if($width<=0||$width>170||$height<=0||$height>200){
$myFile="tempo/".$image_id.".jpg";
$fh=fopen($myFile,'w') or die("The File could not be opened!!!");
fclose($fh);
unlink($myFile);
echo "<script type='text/javascript'>alert('The width of your profile image must be less than 170 px, the height must be less than 200 px!!!')</script>";
}
else
$_POST['image_id']=$fotograf_id;
}
else{
$image_id= md5(uniqid());
$_FILES['profile_foto']['name']="tempo/".$image_id.".jpg";
move_uploaded_file($_FILES['profile_foto']['temp_name'],$_FILES['profile_foto']['name']);
list($width,$height)=getimagesize("tempo/".$image_id.".jpg");
if($width<=0||$width>170||$height<=0||$height>200){
$myFile="tempo/".$image_id.".jpg";
$fh=fopen($myFile,'w') or die("The File could not be opened!!!");
fclose($fh);
unlink($myFile);
echo "<script type='text/javascript'>alert('The width of your profile image must be less than 170 px, the height must be less than 200 px!!!')</script>";
}
else{
$image_will_be_deleted=$_POST['image_id'];
$myFile="tempo/".$image_will_be_deleted.".jpg";
$fh=fopen($myFile,'w') or die("The File cannot be opened!!!");
fclose($fh);
unlink($myFile);
$_POST['image_id']=$image_id;
}
}
}
}
else
echo "<script type='text/javascript'>alert('The size of the profile image must be less than 100 kb!!!')</script>";
}
I ran the code in apache localhost and also tried in my host. In both of them, the method moved the file but the file seemed 0kb.
Here is the code:
if(isset($_POST['upload'])){
if($_FILES['profile_foto']['size']>0&&$_FILES['profile_foto']['size']<102400){
$image_extension=explode("/",$_FILES['profile_foto']['type']);
if($image_extension[1]!="jpeg")
echo "<script type='text/javascript'>alert('The extension of the profile image must be jpeg!!!')</script>";
else{
if($_POST['image_id']==""){
$image_id= md5(uniqid());
$_FILES['profile_foto']['name']="tempo/".$image_id.".jpg";
move_uploaded_file($_FILES['profile_foto']['temp_name'],$_FILES['profile_foto']['name']);
list($width,$height)=getimagesize("tempo/".$image_id.".jpg");
if($width<=0||$width>170||$height<=0||$height>200){
$myFile="tempo/".$image_id.".jpg";
$fh=fopen($myFile,'w') or die("The File could not be opened!!!");
fclose($fh);
unlink($myFile);
echo "<script type='text/javascript'>alert('The width of your profile image must be less than 170 px, the height must be less than 200 px!!!')</script>";
}
else
$_POST['image_id']=$fotograf_id;
}
else{
$image_id= md5(uniqid());
$_FILES['profile_foto']['name']="tempo/".$image_id.".jpg";
move_uploaded_file($_FILES['profile_foto']['temp_name'],$_FILES['profile_foto']['name']);
list($width,$height)=getimagesize("tempo/".$image_id.".jpg");
if($width<=0||$width>170||$height<=0||$height>200){
$myFile="tempo/".$image_id.".jpg";
$fh=fopen($myFile,'w') or die("The File could not be opened!!!");
fclose($fh);
unlink($myFile);
echo "<script type='text/javascript'>alert('The width of your profile image must be less than 170 px, the height must be less than 200 px!!!')</script>";
}
else{
$image_will_be_deleted=$_POST['image_id'];
$myFile="tempo/".$image_will_be_deleted.".jpg";
$fh=fopen($myFile,'w') or die("The File cannot be opened!!!");
fclose($fh);
unlink($myFile);
$_POST['image_id']=$image_id;
}
}
}
}
else
echo "<script type='text/javascript'>alert('The size of the profile image must be less than 100 kb!!!')</script>";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您声称只有一行代码不起作用时,您已经发布了大约 50 行代码。去掉 50 行代码并将其替换为:
....并了解当您尝试上传文件时会发生什么。
C.
You've posted about 50 lines of code when you claim only one isn't working. Strip out the 50 lines of code and replace it with:
....and find out what happens when you try to upload a file.
C.
我认为
应该
在声明中
I think
ought to be
in the statement
如果您想要一个小功能来方便地将照片大小调整为如下所示。如果仅提供了 $size1,则图像将使用它作为其最大尺寸来调整大小,否则,如果提供了 $size2,则图像将使用 $size1 作为其最大尺寸按比例调整大小,然后裁剪其余部分。可能比你的 ($width<=0||$width>170||$height<=0||$height>200) 更容易:
If you want a little function to conveniently resize you photos to whatever see below. if only $size1 is provided the image will be resized using this as its largest dimension, otherwise if $size2 is provided the image will be resized proportionally using $size1 as its largest dimension and then the rest is cropped. Might make it easier than your ($width<=0||$width>170||$height<=0||$height>200):