I've searched the internet for about an hour and found nothing that suited my needs, so i hope you can help me.
story: I have got a web site with a form where you can order something. You´re also able to upload an image if you want to. This image should be transferred (together with a .txt file that contains the details) into a folder which is created. But it doesn't work as it should
My technique:
I only use php for validation. The form is submitted is send to itself and after passing the validation successfully the form data is saved in a session in order to guarantee access for the .php file that handles the data. The file handling the data has three main functions: handle_form (the function called which wraps all other tasks and functions) move_image (the function which should transfer the image) create_auftrag (the function which creates the folder and the .txt file)
Everything works perfectly except the move_image form and I just don´t why. So here´s the code of the file validating the form:
<?php
$error_style = "";
$variante = 0;
$first_time = true;
$varianten_error = "";
$fehler = false;
echo $_POST["varianten"];
if ($_POST["varianten"] == "vorlage") {
$variante = 1;
} elseif ($_POST["varianten"] == "digital") {
$variante = 2;
$varianten_error.= $variante;
} elseif ($_POST["varianten"] == "hardcopy") {
$variante = 3;
$varianten_error.= $variante;
}
if(isset($_POST["form_id"]))
{
include_once("assets/scripts/php/formvalidation.inc.php");
// Neues Testobjekt erstellen
$pruefung= new FormValidation("auftragsformular");
// Prüfungen für Felder definieren
$pruefung->requireField("name", new FieldFilled(), "Bitte geben Sie einen Namen ein.");
$pruefung->requireField("email", new FieldEmail(), "Bitte geben Sie eine Email-Adresse an.");
$pruefung->requireField("plz", new FieldZipCode(), "Bitte geben Sie eine Postleitzahl ein.");
$pruefung->requireField("ort", new FieldFilled(), "Bitte geben Sie einen Ort an.");
$pruefung->requireField("strasse", new FieldFilled(), "Bitte geben Sie eine Straße ein.");
$pruefung->requireField("hausnr", new FieldNumber(), "Bitte geben Sie eine Hausnummer an.");
$pruefung->requireField("hoehe", new FieldNumber(), "Bitte geben Sie die Höhe Ihres Sicherungskastens an.");
$pruefung->requireField("breite", new FieldNumber(), "Bitte geben Sie die Breite Ihres Sicherungskastens an.");
$pruefung->requireField("agb", new FieldFilled(), "Bitte akzeptieren Sie die AGB.");
$pruefung->requireField("varianten", new FieldFilled(), "Bitte wählen Sie eine Variante aus.");
// Prüfung ausführen
if($pruefung->check())
{
// Weiterverarbeitung der Formulardaten
if ($variante == 1) {
if (!isset($_POST['vorlagen'])) {
$varianten_error .= "Bitte wählen sie eine Vorlage aus.";
$fehler = true;
}
} elseif ($variante == 2) {
if (!isset($_FILES['upload'])) {
$varianten_error .= "Bitte laden sie ein Bild hoch.";
$fehler = true;
} elseif ($bildinfo === false) {
$varianten_error .= "Bitte laden sie ein Bild mit korrekter Dateiendung hoch.";
$fehler = true;
}
} elseif ($variante == 3) {
if (!isset($_POST['bildbreite'])) {
$varianten_error .= "Bitte geben sie die Breite ihres Bildes an.";
$fehler = true;
}elseif (!isset($_POST['bildhoehe'])) {
$varianten_error .= "Bitte geben sie die Höhe ihres Bildes an.";
$fehler = true;
}
} elseif (isset($_POST['your_email'])) { // robot field
$fehler = true;
}else {
$fehler = false;
}
$first_time = false;
}
else
{
// Ausgabe der Fehlermeldungen und -styles
$fehler = true;
$error_style = $pruefung->outStyles;
echo $error_style;
$first_time = false;
}
}
if ( $fehler == true ) {
$firma = htmlspecialchars($_POST["firma"]);
$name = htmlspecialchars($_POST["name"]);
$email = htmlspecialchars($_POST["email"]);
$tel = htmlspecialchars($_POST["tel"]);
$plz = htmlspecialchars($_POST["plz"]);
$ort = htmlspecialchars($_POST["ort"]);
$strasse = htmlspecialchars($_POST["strasse"]);
$hausnr = htmlspecialchars($_POST["hausnr"]);
$hoehe = htmlspecialchars($_POST["hoehe"]);
$breite = htmlspecialchars($_POST["breite"]);
$bildhoehe = htmlspecialchars($_POST["bildbreite"]);
$bildbreite = htmlspecialchars($_POST["bildhoehe"]);
$upload = $_FILES["upload"];
echo
"<script type='text/javascript'>
$(document).ready(function(){
$('#errorModal').reveal();
$('#step0').show();
$('#step1').hide();
$('#step2').hide();
$('#senden').hide();
selectStep(0);
});
</script>";
} elseif ($first_time == false and $fehler == false) {
SESSION_START();
$_SESSION["firma"] = htmlspecialchars($_POST["firma"]);
$_SESSION["name"] = htmlspecialchars($_POST["name"]);
$_SESSION["email"] = htmlspecialchars($_POST["email"]);
$_SESSION["tel"] = htmlspecialchars($_POST["tel"]);
$_SESSION["plz"] = htmlspecialchars($_POST["plz"]);
$_SESSION["ort"] = htmlspecialchars($_POST["ort"]);
$_SESSION["strasse"] = htmlspecialchars($_POST["strasse"]);
$_SESSION["hausnr"] = htmlspecialchars($_POST["hausnr"]);
$_SESSION["hoehe"] = htmlspecialchars($_POST["hoehe"]);
$_SESSION["breite"] = htmlspecialchars($_POST["breite"]);
$_SESSION["bildbreite"] = htmlspecialchars($_POST["bildbreite"]);
$_SESSION["bildhoehe"] = htmlspecialchars($_POST["bildhoehe"]);
$_SESSION["upload"] = $_FILES["upload"];
$_SESSION["src"] = $_FILES["upload"]["tmp_name"];
$_SESSION["variante"] = $variante;
$_SESSION["vorlage"] = htmlspecialchars($_POST["vorlagen"]);
header('Location: ../../assets/scripts/php/auftragform.php');
}else {
$firma = "";
$name = "";
$email = "";
$tel = "";
$plz = "";
$ort = "";
$strasse = "";
$hausnr = "";
$hoehe = "";
$breite = "";
$bildhoehe = "";
$bildbreite = "";
$upload = "";
}
?>
and here is the code of the file handling the data. I´m sure all access rights are given because creating the folder and the .txt file works perfectly fine.
<?php
include("minimail.php");
SESSION_START();
$firma = $_SESSION["firma"];
$name = $_SESSION["name"];
$email = $_SESSION["email"];
$tel = $_SESSION["tel"];
$plz = $_SESSION["plz"];
$ort = $_SESSION["ort"];
$strasse = $_SESSION["strasse"];
$hausnr = $_SESSION["hausnr"];
$hoehe = $_SESSION["hoehe"];
$breite = $_SESSION["breite"];
$bildhoehe = $_SESSION["bildhoehe"];
$bildbreite = $_SESSION["bildbreite"];
$variante = $_SESSION["variante"];
$vorlage = $_SESSION["vorlage"];
foreach ($_SESSION as $item => $value) {
echo "$item: $value <br>";
}
foreach ($_SESSION["upload"] as $item => $value) {
echo "$item: $value <br>";
}
function deleteFolder($albumPath){
$items = scandir($albumPath);
foreach($items as $item){
if ($item != "." && $item != "..") {
$itemPath = $albumPath.'/'.$item;
if(is_dir($itemPath)){
// Unterordner löschen
deleteFolder($itemPath);
}else{
// Datei löschen
unlink($itemPath);
}
}
}
rmdir($albumPath);
}
function create_auftrag ($src = ""){
echo "funktion aufgerufen";
$name = $GLOBALS['name'];
$timestamp = time();
$datum = date("d.m.Y",$timestamp);
$oldpath = $_SERVER['DOCUMENT_ROOT']."/Auftraege/";
deleteFolder($oldpath);
mkdir($oldpath);
chmod($oldpath, 0777);
$path = $_SERVER['DOCUMENT_ROOT']."/Auftraege/".$datum.$name."/";
echo($path);
$filename = $datum.$name.".txt";
if(!dirname($path)!= $path) {
if (mkdir ($path))
{ echo "Der Pfad wurde angelegt<br>"; }
else
{ die("Der Ordner für den Auftrag konnte nicht angelegt werden."); }
}
if(file_exists($path.$filename))
{ $filename = $datum.$name."_kopie.txt"; }
$file = fopen($path.$filename, "w");
// in die Datei schreiben
$geschrieben = fwrite($file,
"
Name: $name
Datum: $datum
Uhrzeit: $uhrzeit
Firma: ".$GLOBALS['firma']."
Email: ".$GLOBALS['email']."
Tel. : ".$GLOBALS['tel']."
PLZ : ".$GLOBALS['plz']."
Ort : ".$GLOBALS['ort']."
Strasse : ".$GLOBALS['strasse']."
Hausnr : ".$GLOBALS['hausnr']."
Höhe des Sicherungskastens : ".$GLOBALS['hoehe']."
Breite des Sicherungskastens : ".$GLOBALS['breite']."
Variante : ".$GLOBALS['variante']."
Vorlage : ".$GLOBALS['vorlage']."
Upload : ".$img['name']."
Bildhoehe : ".$GLOBALS['bildhoehe']."
Bildbreite : ".$GLOBALS['bildbreite']."
"
);
// Datei schliessen
fclose($file);
if(isset($img)){
move_image($src, $path);
}
}
function move_image($src, $destination) {
$ziel = $destination.$_SESSION["upload"]["name"];
echo "$src";
echo "$ziel";
while (file_exists($ziel)) {
$ziel = $destination."kopie_".$img["name"];
}
if (rename($src, $ziel)) {
echo "Bildpuload hat geklappt";
} else {
echo "Bildupload hat nicht geklappt";
}
}
function handle_form(){
$error = false;
$variante = $GLOBALS['variante'];
$upload = $GLOBALS['upload'];
if ($variante == 1) {
create_auftrag();
} elseif ($variante == 2) {
$ziel = $_SESSION["upload"]["tmp_name"]."/".$_SESSION["upload"]["name"];
echo "$ziel";
create_auftrag($ziel);
} elseif ($variante == 3) {
create_auftrag();
}
}
handle_form();
?>
If you want to see it live just head over to http://sicherung.wiv-dev.de/auftrag, but don't wonder about the language, I'm German. I know the code is huge and the problem quite fuzzy, but I haven't seen any other way than asking you for help.