我在上传文件时遇到问题,我的代码在这里:
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['DOB']) && isset($_POST['address']) && isset($_POST['phone']) && isset($_FILES['file'])) {
# code...
$n=mysql_real_escape_string(htmlspecialchars($_POST['name']));
$e=mysql_real_escape_string(htmlspecialchars($_POST['email']));
$dob=mysql_real_escape_string(htmlspecialchars($_POST['DOB']));
$a=mysql_real_escape_string(htmlspecialchars($_POST['address']));
$p=mysql_real_escape_string(htmlspecialchars($_POST['phone']));
$t=time();
$d_of_reg=date('D M Y @ H.i.s', $t);
$cv=$_FILES['file']['name'];
// connect to the db here
mysql_connect('localhost','root','') or die('Please we could not connect.. Try-again!!!');
mysql_select_db('joblessmen')or die('Please we could not connect to our database... Try-again!!!');
$sql="INSERT INTO `users`(`name`, `email`, `DOB`, `address`, `phone number`, `id`, `date_of_reg`) VALUES ('$n','$e','$dob','$a','$p','','$d_of_reg')";
if($sql_run=mysql_query($sql))
{
echo "Thank you for registering. Joblessmen";
}
}
所以我想把这些文件送到服务器,当我echo $_FILE['file']['name']
请问我需要做什么时它什么都没告诉我?我的php_fileinfo中的wamp设置已打开,任何帮助都将不胜感激。
请这是上传文件的另一部分,这是无法正常工作我希望它上传一个jpg和doc,docx,pdf文件只有这是我的PHP代码
if ( isset($_FILES['file']['name']) && isset($_POST['name']) && isset($_POST['email']) && isset($_POST['DOB']) && isset($_POST['address']) && isset($_POST['phone'])) {
# code...
$n=mysql_real_escape_string(htmlspecialchars($_POST['name']));
$e=mysql_real_escape_string(htmlspecialchars($_POST['email']));
$dob=mysql_real_escape_string(htmlspecialchars($_POST['DOB']));
$a=mysql_real_escape_string(htmlspecialchars($_POST['address']));
$p=mysql_real_escape_string(htmlspecialchars($_POST['phone']));
$t=time();
$d_of_reg=date('D M Y @ H.i.s', $t);
//$cv=$_FILES['file']['name'];
/*
function upload_cv_check()
{
// Define some variables
$max_size = 200000;
$files_directory = $_SERVER['DOCUMENT_ROOT'].'/seabird/top-menu/cv';
$file_name = basename($_FILES['file']['name']);
$file_size = filesize($_FILES['file']['tmp_name']);
$file_extension = strrchr($_FILES['file']['name'], '.');
// Available extensions
$extensions = array('.doc', '.pdf','.docx');
// Return false if extension is not allowed
if(!in_array($file_extension, $extensions))
return false;
// Return false if file size is over limit
if($file_size > $max_size)
return false;
echo "file must be 2mb";
// Fast way to rewrite file name, do your own
$final_file_name = strtr($file_name, 'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ', 'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');
$final_file_name = preg_replace('/([^.a-z0-9]+)/i', '-', $final_file_name);
// Move the file to your server
move_uploaded_file($_FILES['my_image']['tmp_name'], $files_directory.$final_file_name);
// Here is the link to your file, adapt it
$file = 'http://'.$_SERVER['HTTP_HOST'].'/my_files_directory/'.$final_file_name;
}
*/
/*
$extension=strtolower(substr($cv, strpos($cv, '.') + 1));
$type=$_FILES['file']['type'];
$size=$_FILES['file']['size'];
$tmp_name=$_FILES['file']['tmp_name'];
$max_size = 300000;
if (isset($cv)) {
if (!empty($cv)) {
if (($extension == 'jpg' || $extension == 'jpeg' ) && $type=='image/jpeg' && $type=='image/jpg' && $size<=$max_size) {
$location='cv/';
if (move_uploaded_file($tmp_name, $location)) {
echo "file has been uploaded";
}
}else {echo "there was an error";}
}else{echo "file must be a image format.";}
# code...
}else{echo "please choose a file now";}
*/
move_uploaded_file($tmp_name, $location);
$tmp_name=$_FILES['file']['tmp_name'];
$location='cv/';
// connect to the db here
mysql_connect('localhost','root','') or die('Please we could not connect.. Try-again!!!');
mysql_select_db('joblessmen')or die('Please we could not connect to our database... Try-again!!!');
$sql="INSERT INTO `users`(`name`, `email`, `DOB`, `address`, `phone number`, `id`, `date_of_reg`) VALUES ('$n','$e','$dob','$a','$p','','$d_of_reg')";
if($sql_run=mysql_query($sql))
{
echo "Thank you for registering. Joblessmen";
//echo $cv=$_FILES['file']['name'];
}
}
所以我希望它从任何一个扩展程序上传一个文件,但它不起作用是告诉未定义的索引,我能做什么
答案 0 :(得分:3)
1)确保您提到了enctype="multipart/form-data"
参数
2)您可以通过
访问该文件$file=$_FILES['file']['name'];
$_FILES['name']['tmp_name']
将为您提供文件的临时路径。
然后,您可以使用以下函数将文件从临时路径移动到文件系统。
move_uploaded_file($_FILES['name']['tmp_name'],"Your filesystem path");
答案 1 :(得分:1)
move_uploaded_file($ _ FILES ['file'] ['tmp_name'],“upload /".$_ FILES ['file'] ['name']); $ file = $ _FILES ['file'] ['name'];
答案 2 :(得分:0)
这是一个包含多个测试的代码,可以执行您想要的操作:
// Define some variables
$max_size = 200000;
$files_directory = $_SERVER['DOCUMENT_ROOT'].'/my_files_directory/';
$file_name = basename($_FILES['my_image']['name']);
$file_size = filesize($_FILES['my_image']['tmp_name']);
$file_extension = strrchr($_FILES['my_image']['name'], '.');
// Available extensions
$extensions = array('.png', '.gif', '.jpg');
// Return false if extension is not allowed
if(!in_array($file_extension, $extensions))
return false;
// Return false if file size is over limit
if($file_size > $max_size)
return false;
// Fast way to rewrite file name, do your own
$final_file_name = strtr($file_name, 'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ', 'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');
$final_file_name = preg_replace('/([^.a-z0-9]+)/i', '-', $final_file_name);
// Move the file to your server
move_uploaded_file($_FILES['my_image']['tmp_name'], $files_directory.$final_file_name);
// Here is the link to your file, adapt it
$file = 'http://'.$_SERVER['HTTP_HOST'].'/my_files_directory/'.$final_file_name;
此外,打开表单时需要使用此行:
<form method="your_method" action="your_action" enctype="multipart/form-data">