Ответ 1
Как сказал @Bonner, ответ Fabien неверен, так как вы ищете script для загрузки файлов со страницы вашего сайта на сервер.
Прежде всего помнить, что функция ftp_put() всегда будет перезаписывать существующие файлы. Вместо этого я предлагаю вам взглянуть на PHP move_uploaded_file
код
Это форма. Внутри атрибута action мы указываем файл, который обрабатывает и обрабатывает все файлы. Вам нужно будет использовать значение multipart/form-data для свойства enctype формы.
Я использовал комментарии почти везде для лучшего понимания.
<form action="upload.php" method="post" enctype="multipart/form-data">
File: <input type="file" name="upload-file" size="30" />
<input type="submit" name="submit" value="Upload file" />
</form>
upload.php
<?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 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'];
}
}
}