[英]Why won't this php upload script work?
I have a html form with file input named image that points to a php file with this code: 我有一个HTML表单,其中包含名为image的文件输入,指向带有以下代码的php文件:
$date = date( "Y_m_d_H_i_s_u" );
function upload() {
$info = pathinfo($_FILES['image']['name']);
$target = "uploads/" . $date . $info['extension'];
if(move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
return true;
} else{
return false;
}
}
I want the filename to have the time in it instead of the original filename. 我希望文件名中有时间而不是原始文件名。 I can't figure out why this won't work!
我不知道为什么这行不通! All the uploaded files are named the extension.
所有上传的文件都称为扩展名。 Somehow the date won't work.
日期以某种方式不起作用。
Your scope is wrong for $date
. 您的范围错误
$date
。 You will want to either pass $date
to your function or make it a global varible 您可能需要将
$date
传递给函数或使其成为全局变量
$date = date( "Y_m_d_H_i_s_u" );
function upload($date) {
$info = pathinfo($_FILES['image']['name']);
$target = "uploads/" . $date . $info['extension'];
if(move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
return true;
} else{
return false;
}
}
$date = date( "Y_m_d_H_i_s_u" );
function upload() {
global $date;
$info = pathinfo($_FILES['image']['name']);
$target = "uploads/" . $date . $info['extension'];
if(move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
return true;
} else{
return false;
}
}
This is my observation , you are having scope
issues 这是我的观察,您遇到
scope
问题
$date = date( "Y_m_d_H_i_s_u" );
Try if the date would always change 尝试日期是否总是会改变
function upload() {
$date = date( "Y_m_d_H_i_s_u" );
$info = pathinfo ( $_FILES ['image'] ['name'] );
$target = "uploads/" . $date . $info ['extension'];
if (move_uploaded_file ( $_FILES ['image'] ['tmp_name'], $target )) {
return true;
} else {
return false;
}
}
$date is outside of the scope of your function. $ date不在功能范围内。 There are 2 ways to fix this:
有两种方法可以解决此问题:
$date = date( "Y_m_d_H_i_s_u" );
function upload() {
globel $date;
$info = pathinfo($_FILES['image']['name']);
$target = "uploads/" . $date . $info['extension'];
if(move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
return true;
}
else{
return false;
}
}
$date = date( "Y_m_d_H_i_s_u" );
function upload($date) {
$info = pathinfo($_FILES['image']['name']);
$target = "uploads/" . $date . $info['extension'];
if(move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
return true;
}
else{
return false;
}
}
upload ($date);
您也可以考虑直接返回move_uploaded_file
return move_uploaded_file($_FILES['image']['tmp_name'], $target)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.