Gomo Posted August 1, 2016 Share Posted August 1, 2016 Hello guys, I've written this code for my website so that I can upload pictures & attach description to the them. The problem is, it works fine for me (PC & phone), but whenever my friend tries to upload one himself, he gets an error (btw this is for cars). Could somebody please take a look at it, see if I'm doing something wrong, or maybe have a better suggestion for it? Thanks a bunch! HTML: <div id="main"> <form action="upload.php" method="POST" enctype="multipart/form-data"> <div class="clear"> <br><br> <div class="col3"> <input type="file" name="file[]" id="browse" multiple required/><br /> </div> <div class="col3"> <div id="brprev">Bitte eine Bild auswählen!</div> <br /> </div> <div class="col3"> <div class="inp"> <label>Hersteller</label> <input type="text" name="text1" required> </div><br> <div class="inp"> <label>Model</label> <input type="text" name="text2" required> </div><br> <div class="inp"> <label>Baujahr</label> <input type="text" name="text3" required> </div><br> <div class="inp"> <label>Km Stand</label> <input type="text" name="text4" required> </div><br> <div class="inp"> <label>PS / KW</label> <input type="text" name="text5" required> </div><br> <div class="inp"> <label>Kraftstoff</label> <input type="text" name="text6" required> </div><br> <div class="inp"> <label>Getriebe</label> <input type="text" name="text7" required> </div><br> <div class="inp"> <label>TÜV & AU</label> <input type="text" name="text8" required> </div><br> <div class="inp"> <label>Preis</label> <input type="text" name="text9" required> </div><br> <div class="inp"> <label>Beschreibung</label> <textarea name="text10" required></textarea> </div><br> <input type="submit" value="Hochladen" id="submit"/> </div> </div> </form> <br> <br> <form action="delete.php" method="POST"> Bild Nummer eintragen: <input type="text" name="delid" required> <input type="submit" value="Bild Löschen" id="submit"/> </form> PHP (upload): <?php if(!isset($_POST['text1'])||empty($_POST['text1'])) die("Hersteller not set!"); if(!isset($_POST['text2'])||empty($_POST['text2'])) die("Model not set!"); if(!isset($_POST['text3'])||empty($_POST['text3'])) die("Baujahr not set!"); if(!isset($_POST['text4'])||empty($_POST['text4'])) die("Km Stand not set!"); if(!isset($_POST['text5'])||empty($_POST['text5'])) die("Preis not set!"); $kcc=1; $location = 'Images/'; $id = @file_get_contents($location.'c.txt')+1; file_put_contents($location.'c.txt',$id); $data = $_POST['text1']."\r\n"; $data.= $_POST['text2']."\r\n"; $data.= $_POST['text3']."\r\n"; $data.= $_POST['text4']."\r\n"; $data.= $_POST['text5']."\r\n"; $data.= $_POST['text6']."\r\n"; $data.= $_POST['text7']."\r\n"; $data.= $_POST['text8']."\r\n"; $data.= $_POST['text9']."\r\n"; $data.= str_replace("\n", '', $_POST['text10'])."\r\n"; $data.= sizeof($_FILES['file']['name']).'-images'."\r\n"; file_put_contents($location.$id.'.txt',$data); foreach($_FILES['file']['name'] as $k=>$v){ $name = $_FILES['file']['name'][$k]; $size = $_FILES['file']['size'][$k]; $type = $_FILES['file']['type'][$k]; $extension = pathinfo($name, PATHINFO_EXTENSION); $max_size = 20971520; $tmp_name = $_FILES['file']['tmp_name'][$k]; $error = $_FILES['file']['error'][$k]; if (isset($name) && !empty($name)) { if (($extension=='jpg' || $extension=='jpeg' || $extension=='png') && $size<=$max_size) { move_uploaded_file($tmp_name, $location.$id.'_'.($kcc++).'.png'); echo 'Bild wird hochgeladen!<br>'; } else { echo 'Bitte eine Bild auswählen!<br>'; } } else { echo 'Bild muss ein jpg/jpeg/png Typ sein und kleiner als 20mb!<br>'; } } ?> <script> setTimeout(function(){ window.location.href = 'http://www.google.com'; }, 3000); </script> [Every image upload creates a '.txt' file in the folder "Images" where the info is stored] PHP (delete): <?php $id = isset($_POST['delid'])?intval($_POST['delid']):0; if($id){ if(file_exists('Images/'.$id.'.txt')){ $data = file('Images/'.$id.'.txt')[10]; unlink('Images/'.$id.'.txt'); for($i=1;$i<=$data;$i++){ if(file_exists('Images/'.$id.'_'.$i.'.png')) unlink('Images/'.$id.'_'.$i.'.png'); } }elseif(file_exists('Images/'.$id.'_1.png')) unlink('Images/'.$id.'_1.png'); echo 'Bild wurde gelöscht!'; } ?> <script> setTimeout(function(){ window.location.href = 'http://www.google.com'; }, 3000); </script> I'd really appreciate your help! Quote Link to comment https://forums.phpfreaks.com/topic/301715-error-in-the-code/ Share on other sites More sharing options...
cyberRobot Posted August 1, 2016 Share Posted August 1, 2016 It looks like you are using the file() function as an array here: $data = file('Images/'.$id.'.txt')[10]; I imagine the "[10]" part isn't necessary. Quote Link to comment https://forums.phpfreaks.com/topic/301715-error-in-the-code/#findComment-1535414 Share on other sites More sharing options...
requinix Posted August 1, 2016 Share Posted August 1, 2016 We can definitely tell you better ways to do this, but to tell you why it doesn't work for your friend we would need to know what the error message is. He is trying to upload a valid image, right? JPEG/PNG no more than 20MB? It looks like you are using the file() function as an array here: $data = file('Images/'.$id.'.txt')[10];I imagine the "[10]" part isn't necessary. 10 is a magic number for the line in the file containing the value of $data.= sizeof($_FILES['file']['name']).'-images'."\r\n";from the earlier code. Quote Link to comment https://forums.phpfreaks.com/topic/301715-error-in-the-code/#findComment-1535415 Share on other sites More sharing options...
Gomo Posted August 1, 2016 Author Share Posted August 1, 2016 We can definitely tell you better ways to do this, but to tell you why it doesn't work for your friend we would need to know what the error message is. He is trying to upload a valid image, right? JPEG/PNG no more than 20MB? Error is not format related, most of the time he gets this "Hersteller not set" even tho he fills out every single field. Quote Link to comment https://forums.phpfreaks.com/topic/301715-error-in-the-code/#findComment-1535416 Share on other sites More sharing options...
Jacques1 Posted August 1, 2016 Share Posted August 1, 2016 This is obviously a client-side problem, so we can stare at your code all day long, it won't help. We need specific information. Ask your friend to use a different browser and device. Ask them to record the traffic with the developer tools of their browser. For example, they should provide The complete source code of the page as received by the server The outgoing POST request with all parameters The response from the server That's something we can anaylze. Until then, really all I can say is: pretty strange. Quote Link to comment https://forums.phpfreaks.com/topic/301715-error-in-the-code/#findComment-1535417 Share on other sites More sharing options...
mac_gyver Posted August 1, 2016 Share Posted August 1, 2016 you MUST test if the form submitted any data, before you can reference the data. there's a condition that occurs, most frequently when uploading files, though it can occur with any post method form submission, when the amount of data to be sent is larger then the post_max_size setting. when this occurs, the server aborts the transmission of the data and both the $_POST and $_FILES arrays will be empty. to handle this, you must first detect that a post method form has been submitted, use if($_SERVER['REQUEST_METHOD'] == 'POST'){, then you can detect this specific size condition and tell the user that the size of the submitted data is too large, then if there is $_FILES and $_POST data, you can reference the data to finish your validation logic. next, when you finally do loop over the $_FILES['file'] array, you must test that the ['error'] element is a zero ( UPLOAD_ERR_OK ), before you can use the file information. something like the example from the php.net documentation - foreach ($_FILES["file"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["file"]["tmp_name"][$key]; $name = $_FILES["file"]["name"][$key]; $size = $_FILES["file"]["size"][$key]; // use the uploaded file information here... } } Quote Link to comment https://forums.phpfreaks.com/topic/301715-error-in-the-code/#findComment-1535419 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.