regoch Posted August 2, 2011 Share Posted August 2, 2011 I got this file upload script and working fine, but I wonna limit it only to pdf files and unique name. Any ideas? <?php $uploadLocation = "../pdf/"; $target_path = $uploadLocation . basename( $_FILES['upfile']['name']); if(move_uploaded_file($_FILES['upfile']['tmp_name'], $target_path)) { echo "The file: ". basename( $_FILES['upfile']['name']). " has been uploaded!"; $pdf_name = basename( $_FILES['upfile']['name']); echo $pdf_name; } else{ echo "There was an error uploading the file, please try again!"; } ?> Link to comment https://forums.phpfreaks.com/topic/243638-pdf-upload-form/ Share on other sites More sharing options...
voip03 Posted August 2, 2011 Share Posted August 2, 2011 $allowed = array( 'pdf'); if(in_array($extension, $allowed)){ // extension is good} Link to comment https://forums.phpfreaks.com/topic/243638-pdf-upload-form/#findComment-1250934 Share on other sites More sharing options...
regoch Posted August 2, 2011 Author Share Posted August 2, 2011 where to put it? sorry beginner in php! Link to comment https://forums.phpfreaks.com/topic/243638-pdf-upload-form/#findComment-1250941 Share on other sites More sharing options...
voip03 Posted August 2, 2011 Share Posted August 2, 2011 <?php $uploadLocation = "../pdf/"; $target_path = $uploadLocation . basename( $_FILES['upfile']['name']); $allowed = array( 'pdf'); if(in_array($extension, $allowed)) { // extension is good if(move_uploaded_file($_FILES['upfile']['tmp_name'], $target_path)) { echo "The file: ". basename( $_FILES['upfile']['name']). " has been uploaded!"; $pdf_name = basename( $_FILES['upfile']['name']); echo $pdf_name; } else{ echo "There was an error uploading the file, please try again!";} } ?> http://www.phpfreaks.com/forums/index.php?topic=339907.msg1602483#msg1602483 Link to comment https://forums.phpfreaks.com/topic/243638-pdf-upload-form/#findComment-1250944 Share on other sites More sharing options...
regoch Posted August 3, 2011 Author Share Posted August 3, 2011 Thanks! Here is finally working code! <form action="" method="post" name="fileForm" id="fileForm" enctype="multipart/form-data"> <center> <table> <tr><td style="width:150px;">Naziv PDF-a: </td><td><input name="pdf_ime" type="text" class="textfield" id="pdf_ime" /></td></tr> <tr><td style="width:150px;">Odaberi pdf za upload:</td><td><input name="upfile" type="file" size="36"></td></tr> <tr><td style="width:150px;"></td><td align="lef"><br/><input type="submit" name="Submit" id="Submit" value="Pošalji" /></td></tr> </table></center> </form> <?php $natjecanje_id = $_GET['natjecanje_id']; if (isset($_POST['Submit'])){ $pdf_ime = $_POST['pdf_ime']; if ($pdf_ime == '') { echo "Niste upisali naziv PDF-a"; } else { ?> <div id="result"> <table width="100%"> <?php function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } $pdf_naziv = stripslashes($_FILES['upfile']['name']); $extension = getExtension($pdf_naziv); $extension = strtolower($extension); $ran_name = rand () ; $pdf_naziv = 'pk-zadar'.$ran_name.'.'.$extension; $allowed = array('pdf'); $uploadLocation = "../pdf/$pdf_naziv"; if(in_array($extension, $allowed)) { if(move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadLocation)) { $pdf_ime = $_POST['pdf_ime']; echo " <table width='400px' align='center' cellspacing='0'> <tr bgcolor='#E6e6e6' height='26'> <td align='center' width='100%' colspan='2'><strong>Potvrda</strong></td> </tr> <tr bgcolor='#F6f6f6'> <td align='center' style='border-left:#E6e6e6 1px solid;border-bottom:#E6e6e6 1px solid;'><img src='images/valid.png'></td> <td align='center' width='80'% style='border-right:#E6e6e6 1px solid;border-bottom:#E6e6e6 1px solid;'><strong>".$pdf_naziv." uspješno unesen.</strong></td> </tr> </table><br />"; mysql_query("INSERT INTO pdf SET pdf_naziv='$pdf_naziv', pdf_ime='$pdf_ime', pdf_natjecanje='$natjecanje_id'"); } else{ echo " <table width='400px' align='center' cellspacing='0'> <tr bgcolor='#E6e6e6' height='26'> <td align='center' width='100%' colspan='2'><strong>Potvrda</strong></td> </tr> <tr bgcolor='#F6f6f6'> <td align='center' style='border-left:#E6e6e6 1px solid;border-bottom:#E6e6e6 1px solid;'><img src='images/warning.png'></td> <td align='center' width='80'% style='border-right:#E6e6e6 1px solid;border-bottom:#E6e6e6 1px solid;'><strong>PDF nije unesen.</strong></td> </tr> </table><br />";; } } else { echo "Pokušali ste uploadati datoteku koja nije PDF"; } ?> </table> </div> <?php } } ?> Link to comment https://forums.phpfreaks.com/topic/243638-pdf-upload-form/#findComment-1250987 Share on other sites More sharing options...
voip03 Posted August 3, 2011 Share Posted August 3, 2011 please mark as solved. the topic solved button can be found at the bottom left of the page. thanks Link to comment https://forums.phpfreaks.com/topic/243638-pdf-upload-form/#findComment-1251102 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.