PDA

View Full Version : upload picture help



esthera
02-13-2006, 08:54 AM
her'es my code



$uploadFile1 = $uploadDir . $_FILES['photo1']['name'];
$photo1=$_FILES['photo1']['name'];
$uploadFile2 = $uploadDir . $_FILES['photo2']['name'];
$photo2=$_FILES['photo2']['name'];



how do I actually do the upload - and how could i set the variable of photo1 to be null if the user did not upload the picture. (and same with photo2 -- I actually have 6 and they are all optional - -meaning they don't need to be uploaded)

chump2877
02-13-2006, 10:44 AM
http://www.tizag.com/phpT/fileupload.php

There's no need to set any variables to NULL or whatever you are saying, a file is either downloaded or not....

esthera
02-13-2006, 11:04 AM
but how do I upload multiple files with Different names

and i need to get the filename -- will the filename be null automatically if a file was not uploaded?

chump2877
02-13-2006, 11:17 AM
here's one way: http://phphybrid.com/tutorials/uploading_multiple_files_with_php/1.php


the important parts:


<?
for ($x = 1; $x <= $num; $x++) {

echo 'File '.$x.': <input type="file" name="file'.$x.'" /><br />';

}
?>

then later:


for ($x = 1; $x <= $num; $x++) {

$file = $_FILES['file'.$x];

if (! is_uploaded_file($file['tmp_name'])) {

$messages[$x-1] = 'File '.$x.': No file selected.';

continue;

}

if (! move_uploaded_file($file['tmp_name'],$dir.$file['name'])) {

$messages[$x-1] = 'File '.$x.': Unable to move file.';

continue;

} else {

$messages[$x-1] = 'File '.$x.': Uploaded...';

}

}

esthera
02-13-2006, 11:35 AM
thanks for your help -- teh follwoing is my code (admin.php?cmd=savenews calls the save news function)

for some reason this is only returnign a number 1 when submitting the form.
I need that if the picture is blank it should just leave that variable should be null.

can you tell me what i'm doing wrong with this code:



function addnews(){
?>
<h1>Add News</h1>
<form method="post" action="admin.php?cmd=savenews" enctype="multipart/form-data">
<span class=col1>Title:</span><input type=text name=title><br>
<span class=col1>Full Article:</span><br>
<textarea name=fullarticle width=100%></textarea>
<br>
<span class=col1>Photo 1:</span> <input name="photo1" type="file" /><br>
<span class=col1>Photo 2:</span> <input name="photo2" type="file" /><br>
<span class=col1>Photo 3:</span> <input name="photo3" type="file" /><br>
<span class=col1>Photo 4:</span> <input name="photo4" type="file" /><br>
<span class=col1>Photo 5:</span> <input name="photo5" type="file" /><br>
<span class=col1>Photo 6:</span> <input name="photo6" type="file" /><br>
<input type=submit name=submit value="Add Article">
</form>
<?
}

function savenews(){

//$uploadDir =
$num=6;


$dir = '/public_html/pictures/';

$num = $_POST['num'];

$messages = array();

for ($x = 1; $x <= $num; $x++) {

$file = $_FILES['photo'.$x];

if (! is_uploaded_file($file['tmp_name'])) {

$messages[$x-1] = 'File '.$x.': No file selected.';

continue;

}

if (! move_uploaded_file($file['tmp_name'],$dir.$file['name'])) {

$messages[$x-1] = 'File '.$x.': Unable to move file.';

continue;

} else {

$messages[$x-1] = 'File '.$x.': Uploaded...';

}

}



//foreach ($messages as $msg) {

// echo $msg.'<br />';

//}




$title=addslashes($_POST['title']);
$fullarticle=addslashes($_POST['fullarticle']);


$user_query = mysql_query("INSERT INTO news (title, fullarticle,newsdate,photo1,photo2,photo3,photo4) VALUES ('".$title."', '".$fullarticle."', '".date("Y-m-d H:i:s")."','".$photo1."','".$photo2."','".$photo3."','".$photo4."')");
echo $user_query;


}

chump2877
02-13-2006, 04:46 PM
I think this is right, I didn't test it:


<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>

<BODY>


<?

// file admin.php


function addnews()
{
?><h1>Add News</h1>
<form method="post" action="admin.php" enctype="multipart/form-data">
<span class=col1>Title:</span><input type=text name=title><br>
<span class=col1>Full Article:</span><br>
<textarea name=fullarticle width=100%></textarea>
<br>
<span class=col1>Photo 1:</span> <input name="photo1" type="file" /><br>
<span class=col1>Photo 2:</span> <input name="photo2" type="file" /><br>
<span class=col1>Photo 3:</span> <input name="photo3" type="file" /><br>
<span class=col1>Photo 4:</span> <input name="photo4" type="file" /><br>
<span class=col1>Photo 5:</span> <input name="photo5" type="file" /><br>
<span class=col1>Photo 6:</span> <input name="photo6" type="file" /><br>
<input type=submit name=submit value="Add Article">
</form><?
}

function savenews()
{
$dir = '/public_html/pictures/';

for ($x = 1; $x <= 6; $x++)
{
$file = $_FILES['photo'.$x];

if (!is_uploaded_file($file['tmp_name']))
{
continue;
}

if (!move_uploaded_file($file['tmp_name'],$dir.$file['name']))
{
continue;
}
}

$title=addslashes($_POST['title']);
$fullarticle=addslashes($_POST['fullarticle']);

$query = "INSERT INTO news (title, fullarticle,newsdate,photo1,photo2,photo3,photo4,photo5,photo6) VALUES ('".$title."', '".$fullarticle."', '".date("Y-m-d H:i:s")."','".$_FILES['photo1']['name']."','".$_FILES['photo2']['name']."','".$_FILES['photo3']['name']."','".$_FILES['photo4']['name']."','".$_FILES['photo5']['name']."',,'".$_FILES['photo6']['name']."')";
$result = mysql_query($query);
if (msql_affected_rows($result) < 1)
{
die("The table row was not added.");
}
}



if ($_POST['submit'])
{
savenews();
echo "The article was added!<br><br>";
unset($_POST['submit']);
}

addnews();


?>




</BODY>
</HTML>

esthera
02-13-2006, 04:59 PM
thanks for your help but the names of the files are going in as blank -- do you have any idea why? the pictures are being uploaded though.
(though would you know how to have it rename the picture if a picture with the same name already exists?)

chump2877
02-13-2006, 05:11 PM
the names of the files are going in as blank -- do you have any idea why

yea, you need to open a connection to your database probably.....



though would you know how to have it rename the picture if a picture with the same name already exists?

Use the filesystem functions (http://us2.php.net/manual/en/ref.filesystem.php) to scan your upload directory and fill up an array full of all the file names in the directory....cycle through the array and if $file['name'] already exists, then rename the file you are trying to upload to something else when you move it from the server temp directory..