emediastudios
-
Posts
418 -
Joined
-
Last visited
Posts posted by emediastudios
-
-
Sorry
Changed the code to this
$array =array('photo1','photo2','photo3'); foreach($array as $val){ $myFile = "../news/". $_POST[$val]; if (file_exists($myFile)){ unlink($myFile); } else{ //file doesnt exist }
It still only unlinks photo1 only
-
Used this code
//or $dir = "../news/" $array =array('photo1','photo2','photo3'); foreach($array as $val){ $myFile = "../news/". $_POST[$val]; if (file_exists($_POST[$val])){ unlink($myFile); //or unlink($dir.$_POST[$val]); } else{ //file doesnt exist } }
and it deletes the record but no unlinking?
Were close i know
-
Sorry for sounding stupid but how do i make $myFile = "../news/". $_POST['photo1']; include the other 2 photos
-
Awesome, got rid of photo1, thanks heaps,
How would i include photo2 and photo3 in this code?
Again thank you
-
Hi, have a delete record code that does the job and deletes the record, im trying to unlink the images that are related to that record.
i added a code but it doesnt work.
Here is my delete record code
<?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } if ((isset($_POST['id'])) && ($_POST['id'] != "")) { $deleteSQL = sprintf("DELETE FROM news WHERE id=%s", GetSQLValueString($_POST['id'], "int")); mysql_select_db($database_p2w, $p2w); $Result1 = mysql_query($deleteSQL, $p2w) or die(mysql_error()); $deleteGoTo = "news_deleted.php"; if (isset($_SERVER['QUERY_STRING'])) { $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?"; $deleteGoTo .= $_SERVER['QUERY_STRING']; $myFile = "../images/news/". $_POST['photo1']; if (!empty($_POST['photo1'])) { unlink($myFile); } } header("Location: $deleteGoTo"); } mysql_select_db($database_p2w, $p2w); $query_Recordset1 = "SELECT * FROM news ORDER BY id ASC"; $Recordset1 = mysql_query($query_Recordset1, $p2w) or die(mysql_error()); $row_Recordset1 = mysql_fetch_assoc($Recordset1); $totalRows_Recordset1 = mysql_num_rows($Recordset1); ?>
My photos are included in my form as
<?php <input name="photo1" type="hidden" id="photo1" value="<?php echo $row_Recordset1['photo1']; ?>" /> <input name="photo2" type="hidden" id="photo2" value="<?php echo $row_Recordset1['photo1']; ?>" /> <input name="photo3" type="hidden" id="photo3" value="<?php echo $row_Recordset1['photo1']; ?>" /> ?>
Once i get one file to be unlinked i ll work on an array to include the 3 files.
My error is as follows
Warning: unlink(../images/news/11.jpg) [function.unlink]: No such file or directory in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\delete_news.php on line 97
Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\delete_news.php:97) in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\delete_news.php on line 103
-
Here is my full page
<?php require_once('../Connections/p2w.php'); ?> <?php if (!isset($_SESSION)) { session_start(); } $MM_authorizedUsers = ""; $MM_donotCheckaccess = "true"; // *** Restrict Access To Page: Grant or deny access to this page function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { // For security, start by assuming the visitor is NOT authorized. $isValid = False; // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. // Therefore, we know that a user is NOT logged in if that Session variable is blank. if (!empty($UserName)) { // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. // Parse the strings into arrays. $arrUsers = Explode(",", $strUsers); $arrGroups = Explode(",", $strGroups); if (in_array($UserName, $arrUsers)) { $isValid = true; } // Or, you may restrict access to only certain users based on their username. if (in_array($UserGroup, $arrGroups)) { $isValid = true; } if (($strUsers == "") && true) { $isValid = true; } } return $isValid; } $MM_restrictGoTo = "restricted.php"; if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) { $MM_qsChar = "?"; $MM_referrer = $_SERVER['PHP_SELF']; if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&"; if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) $MM_referrer .= "?" . $QUERY_STRING; $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer); header("Location: ". $MM_restrictGoTo); exit; } ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } if ((isset($_POST['id'])) && ($_POST['id'] != "")) { $deleteSQL = sprintf("DELETE FROM news WHERE id=%s", GetSQLValueString($_POST['id'], "int")); mysql_select_db($database_p2w, $p2w); $Result1 = mysql_query($deleteSQL, $p2w) or die(mysql_error()); $deleteGoTo = "news_deleted.php"; if (isset($_SERVER['QUERY_STRING'])) { $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?"; $deleteGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $deleteGoTo)); } mysql_select_db($database_p2w, $p2w); $query_Recordset1 = "SELECT * FROM news"; $Recordset1 = mysql_query($query_Recordset1, $p2w) or die(mysql_error()); $row_Recordset1 = mysql_fetch_assoc($Recordset1); $totalRows_Recordset1 = mysql_num_rows($Recordset1); ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Prepare 2 Win</title> <!--[if IE 5]> <style type="text/css"> /* place css box model fixes for IE 5* in this conditional comment */ .thrColAbsHdr #sidebar1 { width: 180px; } .thrColAbsHdr #sidebar2 { width: 190px; } </style> <![endif]--> <script type="text/javascript"> <!-- function MM_preloadImages() { //v3.0 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array(); var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++) if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}} } function MM_validateForm() { //v4.0 if (document.getElementById){ var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments; for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]); if (val) { nm=val.name; if ((val=val.value)!="") { if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@'); if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n'; } else if (test!='R') { num = parseFloat(val); if (isNaN(val)) errors+='- '+nm+' must contain a number.\n'; if (test.indexOf('inRange') != -1) { p=test.indexOf(':'); min=test.substring(8,p); max=test.substring(p+1); if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n'; } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; } } if (errors) alert('The following error(s) occurred:\n'+errors); document.MM_returnValue = (errors == ''); } } //--> </script> <script src="../Scripts/AC_RunActiveContent.js" type="text/javascript"></script> <link href="../css/scrollbar.css" rel="stylesheet" type="text/css" /> </head> <body class="thrColAbsHdr"> <div id="container"> <div id="sidebar1"> <script type="text/javascript"> AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width','290','height','162','title','navigation','src','admin','quality','high','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','movie','admin' ); //end AC code </script> <noscript> <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="290" height="162" title="navigation"> <param name="movie" value="admin.swf" /> <param name="quality" value="high" /> <embed src="admin.swf" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="290" height="162"></embed> </object> </noscript> </div> <div id="sidebar2"> <div align="center"><span class="credit">©2007 Prepare To Win - Jonathan Davie - All Rights Reserved.<br /> Website Design by <a href="http://www.emediastudios.com.au" target="_blank">Emedia Studios</a></span></div> </div> <div id="mainContent"><img src="../images/delete_news_title.png" alt="The Philosophy" width="405" height="35" /> <!-- end #mainContent --> </div> <div class="credit" id="mainContent2"><img src="../images/delete_news_title1.png" alt="add news" width="405" height="23" /> There are currently <?php echo $totalRows_Recordset1 ?> records in your database. <br /><div align="left"> <?php if ($totalRows_Recordset1 == 0) { // Show if recordset empty ?> </MM:DECORATION></MM_HIDDENREGION> <span class="credit"> <MM_HIDDENREGION><MM:DECORATION OUTLINE="Show%20If..." OUTLINEID=1>There are currently no news items to delete</MM:DECORATION></MM_HIDDENREGION> </span> <MM_HIDDENREGION><MM:DECORATION OUTLINE="Show%20If..." OUTLINEID=1></MM:DECORATION></MM_HIDDENREGION> <span class="credit"> <MM_HIDDENREGION><MM:DECORATION OUTLINE="Show%20If..." OUTLINEID=1> <?php } // Show if recordset empty ?> </span></div> <?php do { ?> <?php if ($totalRows_Recordset1 > 0) { // Show if recordset not empty ?> <form action="" method="POST" enctype="multipart/form-data" name="news" target="_self" id="news" onsubmit="MM_validateForm('headline','','R','contents','','R');return document.MM_returnValue"> <table width="100%" border="0" background="../images/application_form.png" class="credit"> <tr> <td width="31%" bgcolor="#000066">Headline:</td> <td width="69%" bgcolor="#000066"><label> <input name="id" type="hidden" id="id" value="<?php echo $row_Recordset1['id']; ?>" /> <?php echo $row_Recordset1['headline']; ?></label></td> </tr> <tr> <td>Contents:</td> <td><label><?php echo $row_Recordset1['contents']; ?></label></td> </tr> <tr> <td>Photo 1 Title</td> <td><label><?php echo $row_Recordset1['title1']; ?></label></td> </tr> <tr> <td>Photo 1:</td> <td><img src="../news/<?php echo $row_Recordset1['photo1']; ?>" alt="<?php echo $row_Recordset1['title1']; ?>" width="50" /></td> </tr> <tr> <td>Photo 2 Title:</td> <td><?php echo $row_Recordset1['title2']; ?></td> </tr> <tr> <td>Photo 2:</td> <td><label><img src="../news/<?php echo $row_Recordset1['photo2']; ?>" alt="<?php echo $row_Recordset1['title2']; ?>" width="50" /></label></td> </tr> <tr> <td>Photo 3 Title</td> <td><?php echo $row_Recordset1['title3']; ?></td> </tr> <tr> <td>Photo 3:</td> <td><img src="../news/<?php echo $row_Recordset1['photo3']; ?>" alt="<?php echo $row_Recordset1['title3']; ?>" width="50" /></td> </tr> <tr> <td>Web Link:</td> <td><label><?php echo $row_Recordset1['link']; ?></label></td> </tr> <tr> <td> </td> <td><label> <input name="submit" type="submit" class="fields" id="submit" value="Delete" /> </label></td> </tr> </table> <br /> <br /> </form> <?php } // Show if recordset not empty ?> <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?></div> <!-- end #container --> </div> </body> </html> <?php mysql_free_result($Recordset1); ?>
I want to include a unlink function in there somewhere to delete the three photos
Thanks for the help
-
My fields in my page are
<?php echo $row_Recordset1['photo1']; ?> ...ect..
-
Added your code.
It deletes the record but no files are deleted?
Here is my ful code
<?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } if ((isset($_POST['headline'])) && ($_POST['headline'] != "")) { $deleteSQL = sprintf("DELETE FROM news WHERE headline=%s", GetSQLValueString($_POST['headline'], "text")); mysql_select_db($database_gcproperty, $gcproperty); $Result1 = mysql_query($deleteSQL, $gcproperty) or die(mysql_error()); $deleteGoTo = "news_deleted.php"; if (isset($_SERVER['QUERY_STRING'])) { $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?"; $deleteGoTo .= $_SERVER['QUERY_STRING']; $arrayOfFiles = array($_POST['photos'][0],$_POST['photos'][1],$_POST['photos'][2]); foreach($arrayOfFiles as $file){ unlink($file); } header(sprintf("Location: %s", $deleteGoTo)); } mysql_select_db($database_gcproperty, $gcproperty); $query_Recordset1 = "SELECT * FROM news"; $Recordset1 = mysql_query($query_Recordset1, $gcproperty) or die(mysql_error()); $row_Recordset1 = mysql_fetch_assoc($Recordset1); $totalRows_Recordset1 = mysql_num_rows($Recordset1); ?>
-
I have this code that i use in a different site that deletes one file(photo).
how can i build an array to delete three files photo1, photo2, photo3,.
<?php $myFile = "../images/news/". $_POST['photo']; if (!empty($_POST['photo'])) { unlink($myFile); } else { echo ""; } ?>
-
YOUR DA MAN FixedWell, the easiest way to fix the problem at this point would be just to change that line to this:
<?php $target = "../news/" . basename( $_FILES[$photo]['name']); ?>
Thank you very very much.
Just one last thing, how would i check if file exists?
IS there a little extra code i can add?
-
Did you try the code i posted above ?
I remodified it.
did the same thing champ
-
This line is your culprit:
<?php $target = $target . basename( $_FILES[$photo]['name']); ?>
You need to reinitialize the $target variable each iteration of the loop.
Sorry to be unable to do what you recommend as im still learning php.
Had an idea that that was cuasing problems but how i do as you say?
Thanks for your help
-
Does the same thing ???
what did u change?
-
I have this error in my code that when i upload 3 images it adds the previous file name to the end of the next files name, very wierd.
here is the code
<?php require_once('../Connections/p2w.php'); //This is the directory where images will be saved $target = "../news/"; //This gets all the other information from the form $headline=$_POST['headline']; $contents=$_POST['contents']; $photo1=($_FILES['photo1']['name']); $photo2=($_FILES['photo2']['name']); $photo3=($_FILES['photo3']['name']); $title1=$_POST['title1']; $title2=$_POST['title2']; $title3=$_POST['title3']; $link=$_POST['link']; //Writes the photo to the server for ($x=1;$x<=3;$x++){ $photo ='photo'.$x; $target = $target . basename( $_FILES[$photo]['name']); if (($_FILES[$photo]["type"] == "image/gif") || ($_FILES[$photo]["type"] == "image/jpeg") || ($_FILES[$photo]["type"] == "image/pjpeg") && ($_FILES[$photo]["size"] < 1000000)){ if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ //Tells you if its all ok echo "Success."; //Writes the information to the database mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; } } else { //Gives and error if its not ++$count; } } if ($count > 0){ echo $count .'failed to upload '; } ?>
this is the resulting file names in my news folder after uploading 3 files originally named 11.jpg,12.jpg,19.jpg
11.jpg, 11.jpg12.jpg, 11.jpg12.jpg.19.jpg
-
<?php require_once('../Connections/p2w.php'); //This is the directory where images will be saved $target = "../news/"; //This gets all the other information from the form $headline=$_POST['headline']; $contents=$_POST['contents']; $photo1=($_FILES['photo1']['name']); $photo2=($_FILES['photo2']['name']); $photo3=($_FILES['photo3']['name']); $title1=$_POST['title1']; $title2=$_POST['title2']; $title3=$_POST['title3']; $link=$_POST['link']; //Writes the photo to the server for ($x=1;$x<=3;$x++){ $photo ='photo'.$x; $target = $target . basename( $_FILES[$photo]['name']); if (($_FILES[$photo]["type"] == "image/gif") || ($_FILES[$photo]["type"] == "image/jpeg") || ($_FILES[$photo]["type"] == "image/pjpeg")){ if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ //Tells you if its all ok echo "Success."; //Writes the information to the database mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; } } else { //Gives and error if its not ++$count; } } if ($count > 0){ echo $count .'failed to upload '; } ?>
-
all the file upload and record is inserted, just one strange thing happening now.
file one named in folder 11.jpg, file two named 11.jpg12.jpg, file 3 renamed 11.jpg12.jpg19.jpg ???
-
i get this
Array ( [photo1] => Array ( [name] => Lineup_220907_1024_200792511327.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC93F.tmp [error] => 0 [size] => 213446 ) [photo2] => Array ( [name] => megan_fox_02.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC94F.tmp [error] => 0 [size] => 65963 ) [photo3] => Array ( [name] => megan_fox_05.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC950.tmp [error] => 0 [size] => 73388 ) ) Array ( [photo1] => Array ( [name] => Lineup_220907_1024_200792511327.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC93F.tmp [error] => 0 [size] => 213446 ) [photo2] => Array ( [name] => megan_fox_02.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC94F.tmp [error] => 0 [size] => 65963 ) [photo3] => Array ( [name] => megan_fox_05.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC950.tmp [error] => 0 [size] => 73388 ) ) Array ( [photo1] => Array ( [name] => Lineup_220907_1024_200792511327.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC93F.tmp [error] => 0 [size] => 213446 ) [photo2] => Array ( [name] => megan_fox_02.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC94F.tmp [error] => 0 [size] => 65963 ) [photo3] => Array ( [name] => megan_fox_05.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC950.tmp [error] => 0 [size] => 73388 ) )
-
changed the code to
<?php require_once('../Connections/p2w.php'); //This is the directory where images will be saved $target = "../news/"; //This gets all the other information from the form $headline=$_POST['headline']; $contents=$_POST['contents']; $photo1=($_FILES['photo1']['name']); $photo2=($_FILES['photo2']['name']); $photo3=($_FILES['photo3']['name']); $title1=$_POST['title1']; $title2=$_POST['title2']; $title3=$_POST['title3']; $link=$_POST['link']; //Writes the photo to the server for ($x=1;$x<=3;$x++){ $photo ='photo'.$x; $target = $target . basename( $_FILES[$photo]['name']); if (($_FILES[$photo]["type"] == "image/gif") || ($_FILES[$photo]["type"] == "image/jpeg") || ($_FILES[$photo]["type"] == "image/pjpeg") && ($_FILES[$photo]["size"] < 20000)){ if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ //Tells you if its all ok echo "Success."; //Writes the information to the database mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; } } else { //Gives and error if its not ++$count; } } if ($count > 0){ echo $count .'failed to upload '; } ?>
Get the same echo
3failed to upload
We'll get there
-
Thanks ever so much. so gratefull for your help.
i tried your code and this echoed
3failed to upload
???
-
Thanks for that.
Sorry for being stupid(still learning php) but if my input fieds are named photo1, photo2, and photo3
how would i change the code ubove to suit my form?
Thanks for all your help.
<?php require_once('../Connections/p2w.php');?> <?php if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; } ?> <?php if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000)) { if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; } } else { echo "Invalid file"; }?> <?php if (($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg") && ($_FILES["file"]["size"] < 20000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("../news/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "../news/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } ?>
-
Hi everyone, been working on this code for a while and wish to add a filter that checks for file exist, file size and extensions.
Is there an easy way to add it to my code below.
<?php require_once('../Connections/p2w.php'); //This is the directory where images will be saved $target = "../news/"; //This gets all the other information from the form $headline=$_POST['headline']; $contents=$_POST['contents']; $photo1=($_FILES['photo1']['name']); $photo2=($_FILES['photo2']['name']); $photo3=($_FILES['photo3']['name']); $title1=$_POST['title1']; $title2=$_POST['title2']; $title3=$_POST['title3']; $link=$_POST['link']; //Writes the photo to the server for ($x=1;$x<=3;$x++){ $photo ='photo'.$x; $target = $target . basename( $_FILES[$photo]['name']); if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ //Tells you if its all ok echo "Success."; //Writes the information to the database mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } } ?>
-
Found a problem.
It inserts the record into sql 3 times.
-
i should have seen that lol
put that inside the loop
for ($x=1;$x<=3;$x++){ $photo ='photo'.$x; $target = $target . basename( $_FILES[$photo]['name']); if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ //Tells you if its all ok echo "Success."; //Writes the information to the database mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } }
Works
Thanks heaps, Im so gratefull
-
Changed this a little as i have this code in another site
<?php for ($x=1;$x<=3;$x++){ $photo ='photo'.$x; if(move_uploaded_file($_FILES[$photo . $x]['tmp_name'], $target)){ ?>
Gave me this error
Sorry, there was a problem uploading your file.Sorry, there was a problem uploading your file.Sorry, there was a problem uploading your file.
could there b a problem here
$target = $target . basename( $_FILES['photo']['name']);
[SOLVED] Cannot unlink file
in PHP Coding Help
Posted
found the problem, your the man, thanks so much