|
ClassWork / Computers-for-the-Rest-of-You-F07-UploadPHP
PHP Code for Uploading from LoggersSimple
<?
//print_r($_POST);
//print_r($_FILES);
//This is a function used later on for testing extenstions
//skip it for now
function endsWith( $str, $sub ) {
return ( substr( $str, strlen( $str ) - strlen( $sub ) ) === $sub );
}
//get ready
unset($filename);
//get the files in from the post
if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;
//if no file was sent
if(!isset($_FILES['data_file']))
$error["data_file"] = "File named data_file was not found.";
$filename = urldecode($_FILES['data_file']['name'] );
//this will contain the extra information that you sent over
//if they don't have a path name use the default of an "up"
//folder in the current directory. This requires you to have a
//world writable "up" directory in the same directory as up.php
$pos = strpos($filename,"/home/");
if ($pos === false) {
$filename = "./up/" . $filename;
}
//if they did not send over any filename
if(empty($filename))
$error["filename"] = "The name of the file was not found.";
//if there were no previous problems
if(empty($error))
{
//security measure, only good file extensions get in
if (endsWith($filename, ".png") || endsWith($filename,".jpg") || endsWith($filename,".log")){
//this is it, move and name the file within the server
$result = @move_uploaded_file($_FILES['data_file']['tmp_name'], $filename );
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}else{
$error["extension"] = "Bad File Extension.";
}
}
if(is_array($error))
{
//send back the litany of errors
while(list($key, $val) = each($error))
{
echo $val;
echo "<br>\n";
}
}else{
//send back a sunny message
$size = filesize($tmp );
echo "OK: $size bytes FILENAME: $filename ";
}
?>
Upload a Header First, Then The Data
<?
//print_r($_POST);
//print_r($_FILES);
//THIS IS JUST A LITTLE FUCNTION FOR CHECKING FILE EXTENSIONS
function endsWith( $str, $sub ) {
return ( substr( $str, strlen( $str ) - strlen( $sub ) ) === $sub );
}
//NOT SURE WHY I DID THIS
unset($metadata);
//NOT SURE WHY I DID THIS
if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;
//LOOK FOR THE DATA
if(!isset($_FILES['data_file']))
$error["data_file"] = "Data was not found.";
//LOOK FOR THE METADATA IN THE FORMAT OF: FILENAME|APPEND|PACKET|1|OF|4
$metadata = urldecode($_FILES['data_file']['name'] );
if(empty($metadata))
$error["metadata"] = "Metadata of the was not found.";
//PARSE METADATA
$parts = explode("|",$metadata,6);
//GET FILENAME, ADD DEFAULT PATH OF ./up/ IF IT DOES NO HAVE A PATH
$filename = $parts[0];
$pos = strpos($filename,"/home/");
if ($pos === false) {
$filename = "./up/" . $filename;
}
if(empty($filename))
$error["filename"] = "The name of the was not found.";
//MAKE SURE FOR SECURITY THAT THE FILE EXTENSIONS ARE KOSHER
if (!(endsWith($filename, ".png") || endsWith($filename,".jpg") || endsWith($filename,".log")) )
$error["extension"] = "Bad File Extension. $filename";
$method = "appended";
//CHECK IF THEY WANT TO REPLACE OR APPEND THE FILE
$append = $parts[1];
$whichPacket = $parts[3];
$totalPackets = $parts[4];
if ($whichPacket != "1") { //if they are sending it over in parts append.
$append = "APPEND";
}else{ //if this is the first (and possibly only) packet check for extra metadata for a separate file
if(sizeof($parts) > 5){
$extraData = $parts[5];
//if (strlen($extraDaat)>0){
$dest = fopen($filename.".meta", 'a') ;
if (empty($dest)) $error["append"] = "Could not open file to append";
if(empty($error)){
$tmp = $_FILES['data_file']['tmp_name'];
$src = fopen($tmp, 'r');
$content = fread($src, filesize($tmp ));
fwrite($dest,$extraData);
//stream_copy_to_stream($src, $dest);
fclose($dest);
}
//}
}
}
$pos = strpos($append,"APPEND");
if ($pos === false){ //REPLACE THE FILE
if(empty($error)){
$result = @move_uploaded_file($_FILES['data_file']['tmp_name'], $filename );
$method = "replaced";
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}
}else{ //APPEND THE FILE
$dest = fopen($filename, 'a') ;
if (empty($dest)) $error["append"] = "Could not open file to append";
if(empty($error)){
$tmp = $_FILES['data_file']['tmp_name'];
$src = fopen($tmp, 'r');
$content = fread($src, filesize($tmp ));
fwrite($dest,$content);
//stream_copy_to_stream($src, $dest);
fclose($dest);
}
}
if(is_array($error)){ //SEND BACK THE ERRORS
while(list($key, $val) = each($error))
{
echo $val;
echo "<br>\n";
}
}else{ //SEND BACK THE SUCESSFUL MESSAGE
$size = filesize($tmp );
echo "OK: $size bytes FILENAME: $metadata $method ";
}
?>
|