由于@Bonner表示Fabien的答案不正确,因为您正在寻找一个脚本来将文件从您网站上的页面上传到服务器.
首先要记住的是,ftp_put()功能将永远覆盖现有的文件.相反,我建议您查看PHP move_uploaded_file
码
这是表格.在action属性中,我们指定一个文件,它将处理和处理所有文件.您需要使用表单的enctype属性的multipart / form-data值.
我几乎无处不在,更好的理解.
File:
upload.php的
// Used to determinated if the upload file is really a valid file
$isValid = true;
// The maximum allowed file upload size
$maxFileSize = 1024000;
//Allowed file extensions
$extensions = array('gif', 'jpg', 'jpeg', 'png');
// See if the Upload file button was pressed.
if(isset($_POST['submit'])) {
// See if there is a file waiting to be uploaded
if(!empty($_FILES['upload-file']['name'])) {
// Check for errors
if(!$_FILES['upload-file']['error']) {
// Renamed the file
$renamedFile = strtolower($_FILES['upload-file']['tmp_name']);
// Get the file extension
$fileInfo = pathinfo($_FILES['upload-file']['name']);
// Now vaidate it
if (!in_array($fileInfo['extension'], $extensions)) {
$isValid = false;
echo "This file extension is not allowed";
}
// Validate that the file is not bigger than 1MB
if($_FILES['upload-file']['size'] > $maxFileSize) {
$isValid = false;
echo "Your file's size is to large. The file should not be bigger than 1MB";
}
// If the file has passed all tests
if($isValid)
{
// Move it to where we want it to be
move_uploaded_file($_FILES['upload-file']['tmp_name'], 'uploads/'.$renamedFile);
echo 'File was successfully uploaded!';
}
}
// If there is an error show it
else {
echo 'There was an error file trying to upload the file: '.$_FILES['upload-file']['error'];
}
}
}