File upload issue
Solved
Loic1983
Posted messages
106
Status
Member
-
Loic1983 Posted messages 106 Status Member -
Loic1983 Posted messages 106 Status Member -
Hello everyone,
I followed a tutorial https://antoine-herault.developpez.com/tutoriels/php/upload/ and I might have misunderstood something because it’s not working.
When I try to upload a file, it shows me: file upload failed!
I created an upload.html page that contains:
<form method="POST" action="upload.php" enctype="multipart/form-data">
<!-- We limit the file to 100Ko -->
<input type="hidden" name="MAX_FILE_SIZE" value="100000">
File: <input type="file" name="avatar">
<input type="submit" name="send" value="Send the file">
</form>
and an upload.php page that contains:
<?php
$directory = 'upload/';
$file = basename($_FILES['avatar']['name']);
$max_size = 100000;
$size = filesize($_FILES['avatar']['tmp_name']);
$extensions = array('.png', '.gif', '.jpg', '.jpeg');
$extension = strrchr($_FILES['avatar']['name'], '.');
//Start of security checks...
if(!in_array($extension, $extensions)) //If the extension is not in the array
{
$error = 'You must upload a file of type png, gif, jpg, jpeg, txt or doc...';
}
if($size>$max_size)
{
$error = 'The file is too large...';
}
if(!isset($error)) //If there is no error, we upload
{
//We format the file name here...
$file = strtr($file,
'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ',
'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');
$file = preg_replace('/([^.a-z0-9]+)/i', '-', $file);
if(move_uploaded_file($_FILES['avatar']['tmp_name'], $directory . $file)) //If the function returns TRUE, it worked...
{
echo 'Upload successful!';
}
else //Otherwise (the function returns FALSE).
{
echo 'Upload failed!';
}
}
else
{
echo $error;
}
?>
but it's not working. I might have done something wrong....
Does anyone see what I might have done or forgotten?
Configuration: Windows Vista / Internet Explorer 7.0
I followed a tutorial https://antoine-herault.developpez.com/tutoriels/php/upload/ and I might have misunderstood something because it’s not working.
When I try to upload a file, it shows me: file upload failed!
I created an upload.html page that contains:
<form method="POST" action="upload.php" enctype="multipart/form-data">
<!-- We limit the file to 100Ko -->
<input type="hidden" name="MAX_FILE_SIZE" value="100000">
File: <input type="file" name="avatar">
<input type="submit" name="send" value="Send the file">
</form>
and an upload.php page that contains:
<?php
$directory = 'upload/';
$file = basename($_FILES['avatar']['name']);
$max_size = 100000;
$size = filesize($_FILES['avatar']['tmp_name']);
$extensions = array('.png', '.gif', '.jpg', '.jpeg');
$extension = strrchr($_FILES['avatar']['name'], '.');
//Start of security checks...
if(!in_array($extension, $extensions)) //If the extension is not in the array
{
$error = 'You must upload a file of type png, gif, jpg, jpeg, txt or doc...';
}
if($size>$max_size)
{
$error = 'The file is too large...';
}
if(!isset($error)) //If there is no error, we upload
{
//We format the file name here...
$file = strtr($file,
'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ',
'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');
$file = preg_replace('/([^.a-z0-9]+)/i', '-', $file);
if(move_uploaded_file($_FILES['avatar']['tmp_name'], $directory . $file)) //If the function returns TRUE, it worked...
{
echo 'Upload successful!';
}
else //Otherwise (the function returns FALSE).
{
echo 'Upload failed!';
}
}
else
{
echo $error;
}
?>
but it's not working. I might have done something wrong....
Does anyone see what I might have done or forgotten?
Configuration: Windows Vista / Internet Explorer 7.0