gpt4 book ai didi

php - 如何在 PHP 中上传文件

转载 作者:行者123 更新时间:2023-11-29 01:41:05 24 4
gpt4 key购买 nike

我在上传文件时遇到问题,我的代码在这里:

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'];
}



}

所以我希望它从任一扩展上传文件,但它不起作用,它告诉 undefined index ,请问我该怎么办

最佳答案

1) 确保你提到了 enctype="multipart/form-data" 参数

2) 您可以通过以下方式访问该文件

$file=$_FILES['file']['name'];

$_FILES['name']['tmp_name'] 将为您提供文件的临时路径。

然后您可以使用以下函数将文件从临时路径移动到您的文件系统。

move_uploaded_file($_FILES['name']['tmp_name'],"Your filesystem path");

关于php - 如何在 PHP 中上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21988213/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com