Hello!
I have this code for upload one magazine and at the same time rename the 2 last magazines in server.
The ideia is send the new magazine and when press send button in first place the 2 last archives are renamed in this order:
last.pdf -> old.pdf
new.pdf -> last.pdf
and finally the new file (it does not interest the name) is uploaded and renamed to new.pdf
The same proccess is made to send the layer image of the corresponding magazine.
Now I need one security alert if I only select one field (like: you only input PDF field or IMG field is empty ... wants to continue?), when I press Send Button.
Another question:
This script work fine in PHP4 but in PHP5 the rename function don't work.
I will appreciate if somebody help to improve the code.
Regards
Fernando N.
Code:
<?php
// Global Connection Settings
$ftp_server = "ftp.localhost"; // FTP Server Address (exlucde ftp://)
$ftp_user_name = "user"; // FTP Server Username
$ftp_user_pass = "pass"; // Password
$path = "/path/public_html/online";
//variaveis para pdf
$up_file = "new.pdf";
$act_file = "$path/new.pdf";
$ult_file = "$path/last.pdf";
$ant_file = "$path/old.pdf";
$volby = 2048000; //em bytes (1024000 = 1000Kb = 1Mb)
$vol = $volby / 1024; //converte para KB
//variaveis para img
$up_file2 = "new.jpg";
$act_file2 = "$path/new.jpg";
$ult_file2 = "$path/last.jpg";
$ant_file2 = "$path/old.jpg";
$volby2 = 102400; //em bytes (1024000 = 1000Kb = 1Mb)
$vol2 = $volby2 / 1024; //converte para KB
// set up basic connection
$err =($_FILES['pdf_file']['name'] . ', ' . ceil($_FILES['pdf_file']['size'] / 1024));
$err2 =($_FILES['img_file']['name'] . ', ' . ceil($_FILES['img_file']['size'] / 1024));
// Connect to FTP Server
$conn_id = ftp_connect($ftp_server);
// Login to FTP Server
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// Verify Log In Status
if ((!$conn_id) || (!$login_result)) {
echo "Ligação ao FTP falhou! <br />";
echo "Tentando estabelecer ligação ao $ftp_server para o utilizador $ftp_user_name";
exit;
} else {
echo "O ulilizador $ftp_user_name está ligado em $ftp_server <br />";
}
if(isset($_POST['start_upload']) || $_FILES['pdf_file']['name'] != "" || $_FILES['img_file']['name'] != ""){
$local_file = $_FILES['pdf_file']['tmp_name']; // Defines Name of Local File to be Uploaded
$local_file2 = $_FILES['img_file']['tmp_name'];
$destination_file = "$path/".basename($up_file); // Path for File Upload (relative to your login dir)
$destination_file2 = "$path/".basename($up_file2);
if (preg_match('/\\.(pdf)$/i', $_FILES['pdf_file']['name']) && ($_FILES['pdf_file']['size'] < $volby)) {
$ren = (ftp_rename($conn_id, $ult_file, $ant_file)) && (ftp_rename($conn_id, $act_file, $ult_file));
$upload = ftp_put($conn_id, $destination_file, $local_file, FTP_BINARY); // Enviar o novo arquivo
} else
{
$erro = "<font color=\"#FF0000\"><br>Desculpe, $err Kb não foi adicionado.<br>Apenas arquivos com extenção <b>pdf</b> e de volume inferior a <b>$vol Kb</b> são permitidos.</font><br />";
echo $erro;
}
if (preg_match('/\\.(jpg)$/i', $_FILES['img_file']['name']) && ($_FILES['img_file']['size'] < $volby2)) {
$ren = (ftp_rename($conn_id, $ult_file2, $ant_file2)) && (ftp_rename($conn_id, $act_file2, $ult_file2));
$upload = ftp_put($conn_id, $destination_file2, $local_file2, FTP_BINARY); // Enviar o novo arquivo
} else
{
$erro2 = "<font color=\"#FF0000\"><br>Desculpe, $err2 Kb não foi adicionado.<br>Apenas arquivos com extenção <b>jpg</b> e de volume inferior a <b>$vol2 Kb</b> são permitidos.</font><br />";
echo $erro2;
}
// Verify Upload Status
if (!$upload) {
echo "<h2>O envio de ". $_FILES['pdf_file']['name'] . ' e ' . $_FILES['img_file']['name'] ." falhou!<br>Nenhuma actualização foi efectuada.</h2><br /><br />";
} else {
echo "Sucesso!<br /><u>" . $err . " </u>KB foi transferido e renomeado para <u>" . $up_file . "</u> !<br />Os arquivos <u>last.pdf</u> e <u>old.pdf</u> foram actualizados<br />";
echo "Sucesso!<br /><u>" . $err2 . " </u>KB foi transferido e renomeado para <u>" . $up_file2 . "</u> !<br />Os arquivos <u>last.jpg</u> e <u>old.jpg</u> foram actualizados<br />";
}
ftp_close($conn_id); // Close the FTP Connection
}
?>
<html>
<head>
<script type="text/javascript">
window.onload = function() {
document.getElementById("progress").style.visibility = "hidden";
document.getElementById("prog_text").style.visibility = "hidden";
}
function dispProgress() {
document.getElementById("progress").style.visibility = "visible";
document.getElementById("prog_text").style.visibility = "visible";
}
</script>
<style type="text/css">
<!--
.style2 {color: #FFFFFF}
-->
</style>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" enctype="multipart/form-data">
Seleccione o <b><u>PDF</u></b> a enviar: <input name="pdf_file" type="file" size="35" /><br>
Seleccione a <b><u>IMAGEM</u></b> a enviar: <input name="img_file" type="file" size="35" /> <br>
<input type="submit" name="start_upload" value="Enviar" onClick="dispProgress()" />
</form>
<!-- Link to progress file: see http://www.ajaxload.info/ for animated gifs -->
<img id="progress" src="http://localhost/progress.gif" />
<p id="prog_text" style="display:inline;"> Actualização iniciada!</p>
</body>
<html>