Page 1 of 1

Putting a picture on a news posting site

Posted: Fri Jul 11, 2003 11:23 am
by Bruski
Hello, im have a very difficult time uploading a picture and then displaying it on the website. The trick is, its like a news posting i want to upload a different pic for each member and different info but i have no idea how...ive tried this tutorial but that brought me to a dead end..hopefully you guys can gimme some hints or tips.

This is the main code where everything gets processed

Code: Select all

<?
if($action == "edit" && isset($HTTP_POST_VARS&#1111;'password'])) &#123;
    //obviously you should change this password on the next line
    if($HTTP_POST_VARS&#1111;'password'] == "editpass") &#123;
        //First let's recompile that line with the pipe symbols so we can reinsert it
        $line = $HTTP_POST_VARS&#1111;'date'] . "|" . $HTTP_POST_VARS&#1111;'name'];
        $line .= "|" . $HTTP_POST_VARS&#1111;'age'];
	$line .= "|" . $HTTP_POST_VARS&#1111;'yp'];
	$line .= "|" . $HTTP_POST_VARS&#1111;'map'];
        $line = str_replace("\r\n","<BR>",$line);
        $line .= "\r\n";
        $data = file('../cgi-bin/news/news.txt');
        $data&#1111;$id] = $line;
        //the next line makes sure the $data array starts at the beginning
        reset($data);
        //now we open the file with mode 'w' which truncates the file
        $fp = fopen('../cgi-bin/news/news.txt','w');
        foreach($data as $element) &#123;
            fwrite($fp, $element);
        &#125;
        fclose($fp);
        echo "Item Edited!<BR><BR>\n";
        echo "<a href="$PHP_SELF">Go Back</a>\n";
        exit;
    &#125; else &#123;
        echo "Bad password!\n";
        exit;
    &#125;
&#125;
if($action == "edit") &#123;
    $data = file('../cgi-bin/news/news.txt');
    $element = trim($data&#1111;$id]);
    $pieces = explode("|", $element);
    //the next line is to reverse the process of turning the end of lines into breaking returns
    $news = str_replace("<BR>","\r\n",$pieces&#1111;2]);
    echo "Make the changes you would like and press save.<BR>\n";
    echo "<FORM ACTION="$PHP_SELF?action=edit" METHOD="POST" NAME="editform">\n";
    echo "Name:<BR>\n";
    echo "<INPUT TYPE="text" SIZE="30" NAME="name" value="".$pieces&#1111;1].""><BR>\n";
    echo "Age:<BR>\n";
    echo "<INPUT TYPE="text" SIZE="30" NAME="age" value="".$pieces&#1111;2].""><BR>\n";
    echo "Years Playing:<BR>\n";
    echo "<INPUT TYPE="text" SIZE="30" NAME="yp" value="".$pieces&#1111;3].""><BR>\n";
    echo "Favorite Map:<BR>\n";
    echo "<INPUT TYPE="text" SIZE="30" NAME="map" value="".$pieces&#1111;4].""><BR>\n";
    echo "Password:<BR>\n";
    echo "<INPUT TYPE="password" SIZE="30" NAME="password"><BR>\n";
    echo "<INPUT TYPE="hidden" NAME="date" VALUE="".$pieces&#1111;0]."">\n";
    echo "<INPUT TYPE="hidden" NAME="id" VALUE="$id">\n";
    echo "<INPUT TYPE="submit" NAME="submit" VALUE="Save"><BR>\n";
    echo "</FORM>\n";
    exit;
&#125;
if($action == "delete" && isset($HTTP_POST_VARS&#1111;'password'])) &#123;
    //obviously you should change this password on the next line
    if($HTTP_POST_VARS&#1111;'password'] == "deletepass") &#123;
        $data = file('../cgi-bin/news/news.txt');
        //this next line will remove the single news item from the array
        array_splice($data,$id,1);
        //now we open the file with mode 'w' which truncates the file
        $fp = fopen('../cgi-bin/news/news.txt','w');
        foreach($data as $element) &#123;
            fwrite($fp, $element);
        &#125;
        fclose($fp);   
        echo "Item deleted!<BR><BR>\n";   
        echo "<a href="$PHP_SELF">Go Back</a>\n";
        exit;
    &#125; else &#123;
        echo "Bad password!\n";
        exit;
    &#125;
&#125;
if($action == "delete") &#123;
    echo "<H2>You are about to delete the following news item.</H2>\n";
    $data = file('../cgi-bin/news/news.txt');
    $element = trim($data&#1111;$id]);
    $pieces = explode("|", $element);
    echo $pieces&#1111;1] . "<BR>" . $pieces&#1111;2] . "<BR>" . $pieces&#1111;3] . "<BR>" . $pieces&#1111;4]. "</b>\n";
    echo "<BR><BR>\n";
    echo "Are you sure you want to delete this news item? If so, enter the password and click on Delete.<BR>\n";
    echo "<FORM ACTION="$PHP_SELF?action=delete" METHOD="POST" NAME="deleteform">\n";
    echo "Password:<BR>\n";
    echo "<INPUT TYPE="password" SIZE="30" NAME="password"><BR>\n";
    echo "<INPUT TYPE="hidden" NAME="id" VALUE="$id">\n";
    echo "<INPUT TYPE="submit" NAME="submit" VALUE="Delete"><BR>\n";
    echo "</FORM>\n";
    exit;
&#125;
echo "<H1><u>Current Roster</u></H1>\n";
$data = file('../cgi-bin/news/news.txt');
//next line removed to make everything else easier in the admin script
//$data = array_reverse($data);
foreach($data as $key=>$element) &#123;
    $element = trim($element);
    $pieces = explode("|", $element);

    echo "<center><table cellspacing=0 cellpadding=0 cellwall=o border=0><tr><td><img src=template/name.gif></td><td width=200>" . $pieces&#1111;1] . "</td></tr><tr><td td bgcolor=#E2E2E2><img src=template/age.gif></td><td bgcolor=#E2E2E2 width=200>" . $pieces&#1111;2] . "</td></tr><tr><td><img src=template/yp.gif></td><td width=200 >" . $pieces&#1111;3] . "</td></tr><tr><td bgcolor=#E2E2E2><img src=template/map.gif></td><td bgcolor=#E2E2E2 width=200>" . $pieces&#1111;5] . "</td></tr></table>" . "</b>\n";
    echo "<br> <a href="$PHP_SELF?action=delete&id=$key">Delete</a>\n";
    echo "<a href="$PHP_SELF?action=edit&id=$key">Edit</a>\n";
    echo "<BR><BR>\n";
&#125;
?>
and this is the code where you input the information

Code: Select all

<html>
<body>
<?
//this should all go into one file. I would name it addnews.php

echo "<H1><u>Add News</u></H1>\n";
if($HTTP_POST_VARS&#1111;'submit']) &#123;
    if($HTTP_POST_VARS&#1111;'password'] == 'pass') &#123;
        if(!$HTTP_POST_VARS&#1111;'name']) &#123;
            echo "You must enter a name";
            exit;
	&#125;
	if(!$HTTP_POST_VARS&#1111;'age']) &#123;
            echo "You must enter an age";
            exit;
        &#125;
        if(strstr($HTTP_POST_VARS&#1111;'name'],"|")) &#123;
            echo "Name cannot contain the pipe symbol - |";
            exit;
        &#125;
        if(strstr($HTTP_POST_VARS&#1111;'yp'],"|")) &#123;
            echo "News cannot contain the pipe symbol - |";
            exit;
	&#125;
        if(strstr($HTTP_POST_VARS&#1111;'age'],"|")) &#123;
            echo "News cannot contain the pipe symbol - |";
            exit;
        &#125;
        $fp = fopen('../cgi-bin/news/news.txt','a');
        if(!$fp) &#123;
            echo "Error opening file!";
            exit;
        &#125;
        $line = date("m.d.y") . "|" . $HTTP_POST_VARS&#1111;'name'];
        $line .= "|" . $HTTP_POST_VARS&#1111;'age'];
	$line .= "|" . $HTTP_POST_VARS&#1111;'yp'];
	$line .= "|" . $HTTP_POST_VARS&#1111;'map'];
        $line = str_replace("\r\n","<BR>",$line);
        $line .= "\r\n";
        fwrite($fp, $line);
        if(!fclose($fp)) &#123;
            echo "Error closing file!";
            exit;
        &#125;
        echo "<b>News added!</b>\n";   
    &#125; else &#123;
        echo "Bad Password";
    &#125;
&#125;

?>
<FORM ACTION="<?=$PHP_SELF?>" METHOD="POST" NAME="newsentry">
Name:<BR>
<INPUT TYPE="text" SIZE="30" NAME="name"><BR>
Age:<BR>
<INPUT TYPE="text" SIZE="30" NAME="age"><BR>
Years Playing:<BR>
<INPUT TYPE="text" SIZE="30" NAME="yp"><BR>
Favorite Map:<BR>
<INPUT TYPE="text" SIZE="30" NAME="map"><BR><br>
Password:<br>
<INPUT TYPE="password" SIZE="30" NAME="password"><BR>
<INPUT TYPE="submit" NAME="submit" VALUE="Post it!"><BR>
</FORM>


</body>
</htmL>
any help would be greatly appreciated beacuse its getting on me nerves :?

thanks again,

Bruski

Posted: Fri Jul 11, 2003 12:37 pm
by daven
If you are using a recent version of PHP, you should be using superglobals ($_GET, $_POST,$_REQUEST,$_FILES) rather than the $HTTP_*_VARS globals.

That being said, check out the code in the following post: viewtopic.php?t=10504
Ignore the javascript and FTP functions, they are not necessary for what you want.

Posted: Fri Jul 11, 2003 12:46 pm
by Bruski
is there anyway to just keep what i have right now and just add to it...

Posted: Fri Jul 11, 2003 2:59 pm
by m3rajk
asuming the tree looks like:

------------------ ----------------------
| base dir with |----| pictures sub dir |
| all php files | ----------------------
------------------



then use the username for the session. take that. have all their pics be username.extension

Posted: Fri Jul 11, 2003 11:39 pm
by Bruski
you lost me at the first thing u said...im a noob at php...it took me just about a few days to creat this..what you are saying is just going one side and coming out the other... 8O im sorry but i got no idea what your saying...im such a noob =[

Posted: Sat Jul 12, 2003 9:59 am
by m3rajk
newb to computers or php. the first thing is talking about directory trees. something that should be understood by anyone using programming or scripting languages, hell, even markup languages.

do you know what a directory tree is? i need to find a basis ... where we both know computers, so i can explain relative to that. i might be able to restructure your whole code to something better

Posted: Sat Jul 12, 2003 10:22 am
by Bruski
oh sorry it was like 3 am here when i read that...yea im just a nub with php not with computers...i know what u mean by directory trees...i dont know what u mean by taking that and putting it in "username.extensions"

Posted: Sat Jul 12, 2003 1:31 pm
by m3rajk
doyou work with macs? windows? posix?


in windows everything has an extension. that's how windows knows what's what.

the other two are smart and look at file headers

html in the file name example.html is an extension
those picture files in your "my photos" directory on your windows machine.. the jpg, jpe, jpeg, gif, png, bmp, tiff
those are extensions


username == the person uploading. what's he logged in as?

if you know the files are username.extension or something that's been standardized like that, it's easier to call the files after

Posted: Sat Jul 12, 2003 1:34 pm
by Bruski
yes i get you on that its just i dont know how to use what you have given me ,put it into code and run it ...im using windows...my website is on a windows platform...

Posted: Sat Jul 12, 2003 1:39 pm
by m3rajk
ok
well.. here's my way of dealing with uploaded files for a site i'm making. (note, this is untested)

Code: Select all

<?php
    else{ #only bother if the confcode is right
      # process the uploaded files (if any)

      $images=array('main'=>'Main picture', 't1'=>'1st Thumbnail', 't2'=>'2nd Thumbnail', 't3'=>'3rd Thumbnail', 't4'=>'4th Thumbnail', 'salute'=>'Salute');
      foreach($images as $key=>$value){
	if($_FILES[$key]['name']){ # if they uploaded a file
	  if(153600<$_FILES[$key]['size']){ # make sure it isn't over 150 KB
	    $picerr=TRUE; $warn=TRUE;
	    $warns[]="Your $value was too large. You may not upload a file over 153600 Bytes (150 KB)";
	  }
	  if('image/jpeg'!==$_FILES[$key]['type']){ # only accept jpegs
	    $picerr=TRUE; $warn=TRUE;
	    $warns[]="Your $value was not a JPEG. JPEG encoded files traditionally end with .jpe, .jpg, and .jpeg on windows.";
	  }
	  if($_FILES[$key]['error'] !== (0 || 'UPLOAD_ERR_OK')){ # if there was an error
	    $picerr=TRUE; $warn=TRUE;
	    $warns[]="Uploading your $value caused an error: $error";
	  }
	  if(!($picerr)){ # if there wasn't an issue, move to the awaiting approval bin -- humans will check it's ok
	    $un=$_COOKIE['un']; $to='/var/www/html/findyourdesire/unapproved/'.$key.'.'.$un.'.jpg';
	    move_uploaded_file($_FILES[$key]['tmp'], $to);
	    $warns[]=$value.' was uploaded sucessfully'; # incase something else went wrong
	  }
	}
      }?>
now when i try to display it, someone's called a page using the username, so i can refrence it by which picture, the username, and the extension (which i know is .jpg)
so an example for me is main.$un.jpg

Posted: Sat Jul 12, 2003 9:30 pm
by Bruski
hey thx...ill try it out