Hi, Im having a problem with the script used to upload files to my site. This script has worked fine on hosts such as xenweb and kwix host but on my newest host, I get a cannot copy file error.
<?php
// Make the function for upload
function do_upload() {
// Valid file Mime types / extension
$allowed_types = array(
"audio/mpeg" => "mp3"
// Add more types here if you like
);
// Check to see if file is an allowed extension
if(!array_key_exists($_FILES['userfile']['type'], $allowed_types)) {
die("<center><font color=\"#000000\" size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">Invalid file type! Only MP3 Files are aloud</font></center>");
}
// Set the maximum file size => 204800 = 200kb
$maxfilesize = 110485760;
// Is it under the allowed Max file size?
if($_FILES['userfile']['size'] > $maxfilesize) {
die("<center><font color=\"#000000\" size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">File is too large!</font></center>");
}
// Where are the files going?
$uploaddir = $_SERVER['DOCUMENT_ROOT'] . "/mp3s/";
// What is the files temporary name?
$file = $_FILES['userfile']['tmp_name'];
// What is the files actual name?
$filename = $_FILES['userfile']['name'];
// Check to see if the file allready exists?
if(file_exists($uploaddir . $filename)) {
die("<center><font color=\"#000000\" size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">A file with that name already exists on this server.</font></center>");
} else {
// If the file does not already exist, copy it.
copy($file, $uploaddir.$filename) or die("<center><font color=\"#000000\" size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">Could not copy file.</font></center>");
}
// Put your finished message here
/* The following echo statement will output a table showing the file name, size and type */
$fname = $_FILES['userfile']['name'];
$fsize = $_FILES['userfile']['size'];
$ftype = $_FILES['userfile']['type'];
echo "<div id=\"caption\">RESULT</div>
<div id=\"icon2\"> </div>
<div id=\"result\">
<table width=\"100%\">
<center> File:</font></div></td>
<td width=\"232\"><font color=\"#000000\" size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">$fname has been uploaded !</font></td> </center>
</tr>
<tr> </table>";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>File Upload</title>
<link href="skins/style/style.css" rel="stylesheet" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.style1 {color: #666666}
body {
background-color: #313031;
}
-->
</style>
</head>
<body>
<div id="main">
<div id="caption">UPLOAD FILE</div>
<div id="icon"> </div>
<form method="post" enctype="multipart/form-data" name="form1">
<input type="hidden" name="action" value="do_upload">
File to upload:<center>
<table>
<tr><td><input name="userfile" type="file" class="button" id="userfile"></td></tr>
<tr><td align="center"><br/><input type="submit" name="Submit" value="Upload File!" class="button"></td></tr>
</table></center>
<div align="center"><span class="style5 style1">This Uploader Was Produced By *****</span> </div>
</form>
</body>
</html>
<?php
// If the form has been completed, execute the upload function (above).
if($_POST['action'] == "do_upload") {
do_upload();
}
?>
My host does not have a Public_html folder and when opening FTP, it goes straight to the index directory for the main page. Here I have a folder called mp3s which I want the script to upload.
Can someone please verfiy my code is correct.
Many thanks
Chris