runeveryday
12-07-2010, 07:05 AM
<?php
$allowed_filetypes = array('.jpg','.gif','.bmp','.png');
$max_filesize = 5242888;
$upload_path = '/files';
$filename =$_FILES['userfile']['name'];
$ext = substr($filename,strpos($filename,'.'),strlen($filename)-1);//get the extension form the filename
if(!in_array($ext,$allowed_filetypes))
die('the file you attempted to upload is not allowed.');
if(filesize($_FILES['userfile']['size'])>$max_filesize)
die('the file you attempted to upload is too large.');
if(!is_writable($upload_path)){
die('you cannot upload to the specified directory,please CHMOD it to 777.');
}
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path.$filename)){
echo 'you file upload successful.view the file <a href=".$upload_path.$filename.title="your file">here</a>';
}
else{
echo 'failed';
}
the code is the upload.php,what's wrong with it. it can't upload a jpg image.
the main html
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="userfile" id="file"/>
<button>upload</button>
$allowed_filetypes = array('.jpg','.gif','.bmp','.png');
$max_filesize = 5242888;
$upload_path = '/files';
$filename =$_FILES['userfile']['name'];
$ext = substr($filename,strpos($filename,'.'),strlen($filename)-1);//get the extension form the filename
if(!in_array($ext,$allowed_filetypes))
die('the file you attempted to upload is not allowed.');
if(filesize($_FILES['userfile']['size'])>$max_filesize)
die('the file you attempted to upload is too large.');
if(!is_writable($upload_path)){
die('you cannot upload to the specified directory,please CHMOD it to 777.');
}
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path.$filename)){
echo 'you file upload successful.view the file <a href=".$upload_path.$filename.title="your file">here</a>';
}
else{
echo 'failed';
}
the code is the upload.php,what's wrong with it. it can't upload a jpg image.
the main html
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="userfile" id="file"/>
<button>upload</button>