hi there 🙂
I've been working on an upload script that checks the mime-type and extension to make sure that it is an mp3, then checks from a list of md5 hashes, to make sure it hasn't been uploaded before. The script was working, but then it broke, and I'm not sure why.
<?php
//Сheck that we have a file
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
$filename = basename($_FILES['uploaded_file']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
if (($ext == "mp3") && ($_FILES["uploaded_file"]["type"] == "audio/mpeg")) {
//Determine the path to which we want to save this file
$newname = dirname(__FILE__).'/request_uploads/'.$filename;
//Check if the file with the same name is already exists on the server
if (!file_exists($newname)) {
//Attempt to move the uploaded file to it's new place
if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
$md5 = md5($newname); //create an MD5sum of the uploaded file, to check for duplicates
/*$fp = fopen("/opt/lampp/htdocs/shoutcast webUI/request_uploads/md5_list", "r+") or die("Cannot open the md5 list");
if ($fp) {
while (!feof($fp)) {
$buffer = fgets($fp);
echo $buffer . " " . $md5 . "<br />";
if ($buffer == $md5){
die( $filename . " matches with " . htmlspecialchars($buffer) . " on our system. This means we already have this file. <br />");
}
}
fclose($fp);
}*/
$fp = file_get_contents ("/opt/lampp/htdocs/shoutcast webUI/request_uploads/md5_list");
$fp = explode("###", $fp);
foreach ($fp as &$value) {
if ( $value == $md5 ) {
unlink($newname);
die(htmlspecialchars($filename) . " matches with " . htmlspecialchars($value) . " on our system. This means we already have this file. <br />");
}
}
unset($value);
unset($fp);
echo "Upload complete. The file has been saved as: ".htmlspecialchars($filename).".<br />";
$fp = fopen("/opt/lampp/htdocs/shoutcast webUI/request_uploads/md5_list", 'a') or die("can't open file");
$stringData = $md5 . "###";
fwrite($fp, $stringData);
fclose($fp);
echo "Added the md5 " . $md5 . " to the list.";
} else {
echo "Error: A problem occurred during file upload!";
}
} else {
echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
}
} else {
echo "Error:";
}
} else {
echo "Error: No file uploaded";
}
?>
What is breaking the script?
When I upload, I get: "Error: No file uploaded".