PDA

View Full Version : What would be the code for



GoldenMerc
23-11-2008, 02:33 PM
Say if i wanted just on file name uploaded and not to allow any other file names what would it be, Say i wanted
Banner1.png
Banner2.png

What would be the code to make it so only they could be uploaded, And once uploaded if i wanted to re-upload banner1.png say a updated one it automatically overwrites it.
Ross

Jxhn
23-11-2008, 03:03 PM
Files are automatically overwritten.

if($name != "banner1.jpg" || $name != "banner2.jpg")
{
die("File name not allowed");
}
That should work if there are only one or two names. If there are more then it would probably be a better idea to check with MySQL.

GoldenMerc
23-11-2008, 03:10 PM
<?php require('head.php');

if(!$_GET["upload"])
{
echo('

<form action="?upload=upload" method="post" enctype="multipart/form-data">
<div align="center">
Please Choose a file to upload...<br />
<input type="file" name="file" /><br /><br />
<input type="submit" value="Upload File" />
</div>
</form>

');
}
else
{
$filename = 'uploads/'.$_FILES["file"]["name"].'';
if(!file_exists($filename))
{
move_uploaded_file($_FILES["file"]["tmp_name"],$filename);

$filesize = getimagesize("$filename");

if($filesize[0]<=53 AND $filesize[1]<=53)
{
echo('Your File URL<br /><input type="text" size="50" value="http://www.hhgs.net/radiodjpanel/'.$filename.'" />');
}
else
{
unlink("$filename");
echo('Files must be the dimensions 53*453 or less!');
}
if($name != "banner1.jpg" || $name != "banner2.jpg")
{
die("File name not allowed");
}
?>

So that would work correct?

Jxhn
23-11-2008, 03:25 PM
<?php require('head.php');

if(!$_GET["upload"])
{
echo('

<form action="?upload=upload" method="post" enctype="multipart/form-data">
<div align="center">
Please Choose a file to upload...<br />
<input type="file" name="file" /><br /><br />
<input type="submit" value="Upload File" />
</div>
</form>

');
}
else
{
$filename = 'uploads/'.$_FILES["file"]["name"].'';
if(!file_exists($filename))
{
move_uploaded_file($_FILES["file"]["tmp_name"],$filename);

$filesize = getimagesize("$filename");

if($filesize[0]<=53 AND $filesize[1]<=53)
{
echo('Your File URL<br /><input type="text" size="50" value="http://www.hhgs.net/radiodjpanel/'.$filename.'" />');
}
else
{
unlink("$filename");
echo('Files must be the dimensions 53*453 or less!');
}
if($name != "banner1.jpg" || $name != "banner2.jpg")
{
die("File name not allowed");
}
?>

So that would work correct?

No,


<?php require('head.php');

if(!$_GET["upload"])
{
echo('

<form action="?upload=upload" method="post" enctype="multipart/form-data">
<div align="center">
Please Choose a file to upload...<br />
<input type="file" name="file" /><br /><br />
<input type="submit" value="Upload File" />
</div>
</form>

');
}
else
{
$name=$_FILES["file"]["name"]
$filename = 'uploads/'.$name.'';
if($name == "banner1.jpg" || $name =="banner2.jpg")
{
move_uploaded_file($_FILES["file"]["tmp_name"],$filename);

$filesize = getimagesize("$filename");

if($filesize[0]<=53 AND $filesize[1]<=53)
{
echo('Your File URL<br /><input type="text" size="50" value="http://www.hhgs.net/radiodjpanel/'.$filename.'" />');
}
else
{
unlink("$filename");
echo('Files must be the dimensions 53*453 or less!');
}
}
else
{
echo "File name not allowed";
}

GoldenMerc
23-11-2008, 03:31 PM
Parse error: syntax error, unexpected T_VARIABLE in /home/hhgsnet/public_html/radiodjpanel/ibanner.php on line 20
Doesn't seem to be right.

Jxhn
23-11-2008, 03:53 PM
Uhh, I forgot a semicolon after
$name=$_FILES["file"]["name"]

GoldenMerc
23-11-2008, 04:05 PM
Thats line 19, Its line 20...

wsg14
23-11-2008, 04:16 PM
This should work:

change (this is line 20)

$filename = 'uploads/'.$name.'';
to

$filename = 'uploads/{$name}';

Jxhn
23-11-2008, 04:29 PM
Thats line 19, Its line 20...

Well the semicolon has to be there and I can't see anything wrong with line 20. Try removing the

.''

Trinity
23-11-2008, 05:40 PM
Thats line 19, Its line 20...

The missing semicolon on line 19 would cause it to say there's an error on line 20.

GoldenMerc
23-11-2008, 08:41 PM
same error


Parse error: syntax error, unexpected T_VARIABLE in /home/hhgsnet/public_html/radiodjpanel/ibanner.php on line 20

Source
23-11-2008, 08:49 PM
Post the whole code again, rather than moaning ^_^

GoldenMerc
23-11-2008, 08:50 PM
<?php require('head.php');

if(!$_GET["upload"])
{
echo('

<form action="?upload=upload" method="post" enctype="multipart/form-data">
<div align="center">
Please Choose a file to upload...<br />
<input type="file" name="file" /><br /><br />
<input type="submit" value="Upload File" />
</div>
</form>

');
}
else
{
$name=$_FILES["file"]["name"]
$filename = 'uploads/{$name}';
if($name == "banner1.jpg" || $name =="banner2.jpg")
{
move_uploaded_file($_FILES["file"]["tmp_name"],$filename);

$filesize = getimagesize("$filename");

if($filesize[0]<=53 AND $filesize[1]<=53)
{
echo('Your File URL<br /><input type="text" size="50" value="http://www.hhgs.net/valuesbanners/'.$filename.'" />');
}
else
{
unlink("$filename");
echo('Files must be the dimensions 800*900 or less!');
}
}
else
{
echo "File name not allowed";
}

Jxhn
23-11-2008, 08:56 PM
I uploaded it myself and got a different error, but got it to work by doing this:

<?php require('head.php');

if(!$_GET["upload"])
{
echo('

<form action="?upload=upload" method="post" enctype="multipart/form-data">
<div align="center">
Please Choose a file to upload...<br />
<input type="file" name="file" /><br /><br />
<input type="submit" value="Upload File" />
</div>
</form>

');
}
else
{
$name=$_FILES["file"]["name"];
$filename = 'uploads/'.$name.'';
if($name == "banner1.jpg" || $name =="banner2.jpg")
{
move_uploaded_file($_FILES["file"]["tmp_name"],$filename);

$filesize = getimagesize("$filename");

if($filesize[0]<=53 AND $filesize[1]<=53)
{
echo('Your File URL<br /><input type="text" size="50" value="http://www.hhgs.net/radiodjpanel/'.$filename.'" />');
}
else
{
unlink("$filename");
echo('Files must be the dimensions 53*453 or less!');
}
}
else
{
echo "File name not allowed";
}
}

I forgot the final '}'.
See if it works now.
Works for me: http://johnphptest.freehostia.com/upload.php

GoldenMerc
23-11-2008, 09:11 PM
Thanks il +rep everyone whos helped me, Im going to edit it now so i may pop back and post again :)

GoldenMerc
23-11-2008, 10:10 PM
This is the new code, But its not working now its giving this error:


Parse error: syntax error, unexpected T_STRING in /home/hhgsnet/public_html/radiodjpanel/ibanner.php on line 11
Heres the code +rep to someone who can fix it :)

<?php require('head.php');

if(!$_GET["upload"])
{
echo('

<form action="?upload=upload" method="post" enctype="multipart/form-data">
<div align="center">
Please Choose a file to upload...<br />
The banner displaying here is: <br />
<center><img src='http://hhgs.net/valuesbanners/MemmishCasiBannerPreTrans2712917281.gif' width='312' height='50'>
</center>
<br />
<input type="file" name="file" /><br /><br />
<input type="submit" value="Upload File" />
</div>
</form>

');
}
else
{
$name=$_FILES["file"]["name"];
$filename = 'uploads/'.$name.'';
if($name == "banner1.jpg" || $name =="banner2.jpg")
{
move_uploaded_file($_FILES["file"]["tmp_name"],$filename);

$filesize = getimagesize("$filename");

if($filesize[0]<=53 AND $filesize[1]<=53)
{
echo('Your File URL<br /><input type="text" size="50" value="http://www.hhgs.net/valuesbanners/'.$filename.'" />');
}
else
{
unlink("$filename");
echo('Files must be the dimensions 800*900 or less!');
}
}
else
{
echo "File name not allowed";
}
}

Blinger1
23-11-2008, 10:19 PM
This is the new code, But its not working now its giving this error:

Heres the code +rep to someone who can fix it :)

<?php require('head.php');

if(!$_GET["upload"])
{
echo('

<form action="?upload=upload" method="post" enctype="multipart/form-data">
<div align="center">
Please Choose a file to upload...<br />
The banner displaying here is: <br />
<center><img src='http://hhgs.net/valuesbanners/MemmishCasiBannerPreTrans2712917281.gif' width='312' height='50'>
</center>
<br />
<input type="file" name="file" /><br /><br />
<input type="submit" value="Upload File" />
</div>
</form>

');
}
else
{
$name=$_FILES["file"]["name"];
$filename = 'uploads/'.$name.'';
if($name == "banner1.jpg" || $name =="banner2.jpg")
{
move_uploaded_file($_FILES["file"]["tmp_name"],$filename);

$filesize = getimagesize("$filename");

if($filesize[0]<=53 AND $filesize[1]<=53)
{
echo('Your File URL<br /><input type="text" size="50" value="http://www.hhgs.net/valuesbanners/'.$filename.'" />');
}
else
{
unlink("$filename");
echo('Files must be the dimensions 800*900 or less!');
}
}
else
{
echo "File name not allowed";
}
}

you can't use ' on line 11 because it is ending the echo

Trinity
23-11-2008, 10:20 PM
<?php require('head.php');

if(!$_GET["upload"])
{
echo('

<form action="?upload=upload" method="post" enctype="multipart/form-data">
<div align="center">
Please Choose a file to upload...<br />
The banner displaying here is: <br />
<center><img src=\'http://hhgs.net/valuesbanners/MemmishCasiBannerPreTrans2712917281.gif\' width=\'312\' height=\'50\'>
</center>
<br />
<input type="file" name="file" /><br /><br />
<input type="submit" value="Upload File" />
</div>
</form>

');
}
else
{
$name=$_FILES["file"]["name"];
$filename = 'uploads/'.$name.'';
if($name == "banner1.jpg" || $name =="banner2.jpg")
{
move_uploaded_file($_FILES["file"]["tmp_name"],$filename);

$filesize = getimagesize("$filename");

if($filesize[0]<=53 AND $filesize[1]<=53)
{
echo('Your File URL<br /><input type="text" size="50" value="http://www.hhgs.net/valuesbanners/'.$filename.'" />');
}
else
{
unlink("$filename");
echo('Files must be the dimensions 800*900 or less!');
}
}
else
{
echo "File name not allowed";
}
}

GoldenMerc
23-11-2008, 10:52 PM
Now this has gone wrong too with this error:


Parse error: syntax error, unexpected T_STRING in /home/hhgsnet/public_html/radiodjpanel/ibanner.php on line 13


Heres the new code:

<?php require('head.php');

if(!$_GET["upload"])
{
echo('

<form action="?upload=upload" method="post" enctype="multipart/form-data">
<div align="center">
Please Choose a file to upload...<br />
The banner displaying here is: <br />
<center><img src=\'http://hhgs.net/valuesbanners/banner1.gif\' width=\'312\' height=\'50\'>
</center>
To update the banner make sure the file name is <b> 'banner1.gif'</b> <br />
<br />
<input type="file" name="file" /><br /><br />
<input type="submit" value="Upload File" />
</div>
</form>

');
}
else
{
$name=$_FILES["file"]["name"];
$filename = 'uploads/'.$name.'';
if($name == "banner1.jpg")
{
move_uploaded_file($_FILES["file"]["tmp_name"],$filename);

$filesize = getimagesize("$filename");

if($filesize[0]<=53 AND $filesize[1]<=53)
{
echo('Your File URL<br /><input type="text" size="50" value="http://www.hhgs.net/valuesbanners/'.$filename.'" />');
}
else
{
unlink("$filename");
echo('Files must be the dimensions 800*900 or less!');
}
}
else
{
echo "File name not allowed";
}
}

Trinity
23-11-2008, 10:54 PM
<?php require('head.php');

if(!$_GET["upload"])
{
echo('

<form action="?upload=upload" method="post" enctype="multipart/form-data">
<div align="center">
Please Choose a file to upload...<br />
The banner displaying here is: <br />
<center><img src=\'http://hhgs.net/valuesbanners/banner1.gif\' width=\'312\' height=\'50\'>
</center>
To update the banner make sure the file name is <b> \'banner1.gif\'</b> <br />
<br />
<input type="file" name="file" /><br /><br />
<input type="submit" value="Upload File" />
</div>
</form>

');
}
else
{
$name=$_FILES["file"]["name"];
$filename = 'uploads/'.$name.'';
if($name == "banner1.jpg")
{
move_uploaded_file($_FILES["file"]["tmp_name"],$filename);

$filesize = getimagesize("$filename");

if($filesize[0]<=53 AND $filesize[1]<=53)
{
echo('Your File URL<br /><input type="text" size="50" value="http://www.hhgs.net/valuesbanners/'.$filename.'" />');
}
else
{
unlink("$filename");
echo('Files must be the dimensions 800*900 or less!');
}
}
else
{
echo "File name not allowed";
}
}

GoldenMerc
23-11-2008, 10:59 PM
Right !
I get it, Im trying to repeat this 7 times in 1 page so how would i go around doing that?

HotelUser
24-11-2008, 04:22 AM
I tried the above code and it didn't work. I fixed it and allowed for 7 uploads:


<?php require('head.php');
if(!isset($_POST['submit']))
{
echo'<div align="center">
Please Choose a file to upload...<br />
The banner displaying here is: <br />
<center><img src=\'http://hhgs.net/valuesbanners/banner1.gif\' width=\'312\' height=\'50\'>
</center>
To update the banner make sure the file name is <b> \'banner1.gif\'</b> <br />
<form enctype="multipart/form-data" action="" method="POST">
Choose images to upload:<br />
<input name="uploadedfile1" type="file" /><br />
<input name="uploadedfile2" type="file" /><br />
<input name="uploadedfile3" type="file" /><br />
<input name="uploadedfile4" type="file" /><br />
<input name="uploadedfile5" type="file" /><br />
<input name="uploadedfile6" type="file" /><br />
<input name="uploadedfile7" type="file" /><br />
<input name="submit" type="submit" value="Upload Files" />
</form></div>';
} else {
for ($i=1; $i<=7; $i++)
{
if(basename( $_FILES['uploadedfile'.$i]['name']) == "banner{$i}.jpg" && basename( $_FILES['uploadedfile'.$i]['name']) != "")
{
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile'.$i]['name']);
if(move_uploaded_file($_FILES['uploadedfile'.$i]['tmp_name'], $target_path))
{
list($w, $h) = getimagesize($target_path);
if($w<=800 && $h<=900)
{
echo('Your image URL:<br /><input type="text" size="50" value="http://www.hhgs.net/valuesbanners/'.$target_path.'" /><br /><br />');
} else {
unlink($target_path);
echo('Images must be the dimensions 800*900 or less!<br>');
}
} else {
echo "There was an error uploading the image {$i}, please try again!";
}
} else {
echo "The name of image {$i} must be banner{$i}.jpg!<br />";
}
}
}
?>

Want to hide these adverts? Register an account for free!