<?php
//This variable specifies relative path to the folder, where the gallery with uploaded files is located.
//Do not forget about the slash in the end of the folder name.
$galleryPath = "d:/...";
$absGalleryPath = realpath($galleryPath) . "/";
$absThumbnailsPath = realpath($galleryPath . "Thumbnails/") . "/";
function saveUploadedFiles()
{
global $absGalleryPath, $absThumbnailsPath;
//First of all, clear files and data uploaded at previous time.
//Delete source files.
//$handle = opendir($absGalleryPath);
//while (false !== ($file = readdir($handle)))
//{
//if (is_file($absGalleryPath . $file))
//{
//unlink($absGalleryPath . $file);
//}
//}
//closedir($handle);
//Delete thumbnails
//$handle = opendir($absThumbnailsPath);
//while (false !== ($file = readdir($handle)))
//{
//if (is_file($absThumbnailsPath . $file))
//{
//unlink($absThumbnailsPath . $file);
//}
//}
//closedir($handle);
//NOTE: If you do not want to delete previously uploaded files, just
//remove or comment out the code above.
//Create XML file which will keep information about files (image dimensions, description, etc).
//XML is used solely for brevity. In real-life application most likely you will use database instead.
$descriptions = new DOMDocument('1.0');
$descriptions->appendChild($descriptions->createElement("files"));
//Get total number of uploaded files (all files are uploaded in a single package).
$fileCount = $_POST["FileCount"];
$author = $_GET ["id"];
$id_spot = $_POST['id_spot'];
$prezzo = $_POST['prezzo'];
$valuta = $_POST['valuta'];
$ID_Album = $_GET['id_album'];
$id = $_GET['id'];
$dataalbum = $_POST['data'];
//This value will keep a total size of actually uploaded images. It will be displayed
//so that you could compare how many bytes you save.
$totalUploadedFileSize = 0;
$db_host = "localhost";
$db_user = "";
$db_password = "";
$db_name = "";
$conn = mysql_connect($db_host, $db_user, $db_password);
if ($conn == FALSE)
die ("Errore nella connessione. Verificare i parametri nel file");
mysql_select_db($db_name, $conn)
or die ("Errore nella selezione del database. Verificare i parametri nel file");
$query="Insert Into tb_album (id_album,ID_account,Data_album) values ('$ID_Album','$id','$dataalbum')";
$result = mysql_query ($query, $conn) or die(mysql_error());
//include the S3 class
if (!class_exists('S3'))require_once('S3.php');
//AWS access info
if (!defined('awsAccessKey')) define('awsAccessKey', 'Accesskey');
if (!defined('awsSecretKey')) define('awsSecretKey', 'SecretKey');
//instantiate the class
$s3 = new S3(awsAccessKey, awsSecretKey);
$s3->putBucket("Bucket name", S3::ACL_PUBLIC_READ);
//Iterate through uploaded data and save the original file, thumbnail, and description.
for ($i = 1; $i <= $fileCount; $i++)
{
//Get the first thumbnail and save it to disk. It stores 1200x1200 optimized image.
$thumbnail1Field = "Thumbnail1_" . $i;
if (!$_FILES[$thumbnail1Field]['size'])
{
return;
}
$fileName = getSafeFileName(basename($_POST["FileName_" . $i]));
//move the file
$s3->putObjectFile($_FILES[$thumbnail1Field]['tmp_name'], "folder", $ID_Album . "/" . "Filebig" . "/" . $fileName, S3::ACL_PUBLIC_READ);
$thumbnail2Field = "Thumbnail2_" . $i;
if (!$_FILES[$thumbnail2Field]['size'])
{
return;
}
$s3->putObjectFile($_FILES[$thumbnail2Field]['tmp_name'], "folder", $ID_Album . "/" . "Filesmall" . "/" . $fileName, S3::ACL_PUBLIC_READ);
//move_uploaded_file($_FILES[$thumbnail1Field]['tmp_name'], $absGalleryPath . "/" . $fileName);
//$totalUploadedFileSize = $totalUploadedFileSize + filesize($absGalleryPath . "/" . $fileName);
$ID_Album = $_GET['id_album'];
$id = $_GET['id'];
$dataalbum = $_POST['data'];
//$size=filesize($absGalleryPath . "/" . $fileName);
$date = date("Y-m-d");
//$datefoto = $_POST['datafoto'];
//$timefoto = $_POST['orafoto'];
$query2="Insert Into tb_immagini (NomeFoto,ID_account,SizeFoto,Datainserimento,Datafoto,OraFoto,Convalida,ID_Album,dataalbum,id_spot,prezzo,valuta) values ('$fileName','$id','$size','$date','$datefoto','$timefoto','1','$ID_Album','$dataalbum','$id_spot','$prezzo','$valuta')";
$result2 = mysql_query ($query2, $conn) or die(mysql_error());
//Get the second thumbnail and save it to disk. It stores 120x120 preview image.
//move_uploaded_file($_FILES[$thumbnail2Field]['tmp_name'], $absGalleryPath . "/Thumbnails/" . $fileName . ".jpg");
//Save file info.
$xmlFile = $descriptions->createElement("file");
$xmlFile->setAttribute("name", $fileName);
$xmlFile->setAttribute("width", $_POST ['Width_' . $i]);
$xmlFile->setAttribute("height", $_POST ['Height_' . $i]);
$xmlFile->setAttribute("author", $author);
$descriptions->documentElement->appendChild($xmlFile);
}
//Write both optimized and source file sizes for comparison.
$descriptions->documentElement->setAttribute("totalSourceFileSize", $_POST ['TotalSourceFileSize']);
$descriptions->documentElement->setAttribute("totalUploadedFileSize", ceil($totalUploadedFileSize / 1024));
$descriptions->save($absGalleryPath . "Descriptions.xml");
}
//This method verifies whether file with such name already exists
//and if so, construct safe filename name (to avoid collision).
function getSafeFileName($fileName)
{
global $absGalleryPath;
$newFileName = $fileName;
$j = 1;
while (file_exists($absGalleryPath . "/" . $newFileName))
{
$newFileName = $j . "_" . $fileName;
$j = $j + 1;
}
return $newFileName;
}
saveUploadedFiles();
?>
<script runat="server">
//This variable specifies relative path to the folder, where the gallery with uploaded files is located.
//Do not forget about the slash in the end of the folder name.
private string galleryPath = "D:/home/seeyousurf.com/httpdocs/myphoto/";
private void Page_Load(System.Object sender, System.EventArgs e)
{
//First of all, clear data uploaded at previous time.
//Delete source files
DirectoryInfo dir = new DirectoryInfo(Server.MapPath(galleryPath));
foreach (FileInfo file in dir.GetFiles())
{
file.Delete();
}
//Delete thumbnails
dir = new DirectoryInfo(Server.MapPath(galleryPath) + "/Thumbnails");
foreach (FileInfo file in dir.GetFiles())
{
file.Delete();
}
//NOTE: If you do not want to delete previously uploaded files, just
//remove or comment out the code above.
//Create XML file which will keep information about files (image dimensions, description, etc).
//XML is used solely for brevity. In real-life application most likely you will use database instead.
XmlDocument descriptions = new XmlDocument();
descriptions.AppendChild(descriptions.CreateElement("files"));
//Get total number of uploaded files (all files are uploaded in a single package).
int fileCount = Int32.Parse(Request.Form["FileCount"]);
//This value will keep a total size of actually uploaded images. It will be displayed
//so that you could compare how many bytes you save.
long totalUploadedFileSize = 0;
//Iterate through uploaded data and save the large and small thumbnails.
for (int i = 1; i <= fileCount; i++)
{
//Get the first thumbnail and save it to disk. It stores 1200x1200 optimized image.
HttpPostedFile thumbnail1File = Request.Files["Thumbnail1_" + i];
string fileName = GetSafeFileName(Path.GetFileName(Request.Form["FileName_" + i]));
thumbnail1File.SaveAs(Server.MapPath(galleryPath + fileName));
totalUploadedFileSize += thumbnail1File.InputStream.Length;
//Get the second thumbnail and save it to disk. It stores 120x120 preview image.
HttpPostedFile thumbnail2File = Request.Files["Thumbnail2_" + i];
thumbnail2File.SaveAs(Server.MapPath(galleryPath + "Thumbnails/" + fileName + ".jpg"));
//Save file info.
XmlElement xmlFile = descriptions.CreateElement("file");
xmlFile.SetAttribute("name", fileName);
xmlFile.SetAttribute("width", Request.Form["Width_" + i]);
xmlFile.SetAttribute("height", Request.Form["Height_" + i]);
descriptions.DocumentElement.AppendChild(xmlFile);
}
//Write both optimized and source file sizes for comparison.
descriptions.DocumentElement.SetAttribute("totalSourceFileSize", Request.Form["TotalSourceFileSize"]);
descriptions.DocumentElement.SetAttribute("totalUploadedFileSize", Convert.ToString(Math.Ceiling((double)(totalUploadedFileSize / 1024))));
//Save the description XML file.
descriptions.Save(Server.MapPath(galleryPath + "Descriptions.xml"));
}
//This method verifies whether file with such name already exists
//and if so, construct safe filename name (to avoid collision).
private string GetSafeFileName(string fileName)
{
string newFileName = fileName;
int j = 1;
while (File.Exists(Server.MapPath(galleryPath + newFileName)))
{
newFileName = j + "_" + fileName;
j++;
}
return newFileName;
}
</script>
Thanks
regards