Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    Inserimento immagine di default se campo file vuoto

    ok , questo e'il mio problema

    Ho un campo file all'interno di una form

    ammettiamo che il campo non viene riempito e non viene inviato nessun file, posso io impostare nel file php al quale viene invitao il form un controllo tale che mi inserisce una immagine di default?

    Mi spiego meglio, il campo file si chiama "userfile" e nel php vorrei inserire una riga di questo genere, la variabile $add e' quella che funzione nello script :


    codice:
    
    <body bgcolor=white>
    
    <basefont size=2 face=arial>
    
    Add Article
    
    
    <?
    
            include ("template.inc");
            include ("config.php");
    
    
    if ($userfile_size >500000){$msg=$msg."Your uploaded file size is more than 500KB so please reduce the 
    
    file size and then upload. Visit the help page to know how to reduce the file size.
    ";
    $file_upload="false";}
    
    if (!($userfile_type =="image/pjpeg" OR $userfile_type=="image/gif" OR 
    
    $userfile_type=="image/png")){$msg=$msg."Your uploaded file must be of JPG or GIF. Other file types are 
    
    not allowed
    ";
    $file_upload="false";}
    
    
    
    
    if  ($userfile = "") { $add = "empty.jpg"; } else { $add = "$userfile_name"; } 
    // the path with the file name where the file will be stored, upload is the directory name. 
    
    
    
    if(move_uploaded_file ($userfile, $add)){
    // do your coding here to give a thanks message or any other thing.
    }else{echo "";}
    
    	$summary_template = "t_summary.html";
    	$article_template = "t_article.html";
    	$max_summary = 5;
    
    	function summary_page ($subject, $date, $summary, $article_id, $add)
    	{
    		global $summary_template;
            	$t = new Template();
            	$t->set_file("SummaryPage", $summary_template);
    		$article_url = "article_".$article_id.".html";
    		$date = nl2br($date);
    		$summary =  nl2br($summary);
    		$userfile = $add;
    		$t->set_var( array(
    				"subject" => $subject,
    				"date"    => $date,
    				"summary" => $summary,
    				"userfile" => $userfile,
    				"article_url" => $article_url
    				));
    		$t->parse("Summary", "SummaryPage");
    		return $t->get_var("Summary");
    	}
    
    	function main_page ($subject, $date, $summary, $article_id, $body, $add)
    	{
    		global $article_template;
    
                    $t = new Template();
                    $t->set_file("ArticlePage", $article_template);
                    $article_url = "article_".$article_id.".html";
                    $date = nl2br($date);
                    $summary =  nl2br($summary);
                    $body =  nl2br($body);
    		$userfile = $add;
    		$t->set_var( array(
                                    "subject" => $subject,
                                    "date"    => $date,
                                    "summary" => $summary,
                                    "body" => $body,
    				"userfile" => $userfile,
    				"article_url" => $article_url
                                    ));
                    $t->parse("Article", "ArticlePage");
                    return $t->get_var("Article"); 
    	}
    
    	function add_article($filename, $news)
    	{
    
    		if(file_exists($filename)){
    			$fh = fopen($filename, "r");
    			$old_news = fread($fh, filesize($filename));
    			fclose($fh); 
    		}
    
    		/* TODO: Multipage articles
    			preg_match_all("", $old_news, $matches;
    		
    			if( count($matches[0]) >= $max_summary){
    				$oldfilename = $filename.($matches[0][0]+1);
    			} 
    		*/
    
    		$fh = fopen($filename, "w");
    		$news = stripslashes($news);
    		fwrite($fh, "\n\n$news $old_news");
    		fclose($fh);
    	}
    
    
    		if(strcmp($subject, "")){
    		if(!(strcmp($passwd, $password))){	
    			add_article("article_summary.html", summary_page($subject, $date, $summary, 
    
    $article_id, $add));
    			add_article("article_$article_id.html", main_page($subject, $date, $summary, 
    
    $article_id, $body, $add));
    			echo "
    
     Article has been added! 
    
    ";
    		}else{
    			echo "
    
     Password is wrong! ";
    		}
    	}
    ?>
    
    
    
    
    <form enctype=multipart/form-data action=news.php name=postnews method=post onSubmit="return 
    
    (confirm('Are you sure you wanna send the message?'));">
    <table border=0>
    <tr> <td> Password: </td><td> <input type=password name=passwd size=30> </td></tr>
    <tr> <td> Re-type Password: </td><td> <input type=password name=passwd2 size=30> </td></tr>
    <tr> <td> Subject: </td><td> <input type=text name=subject size=30> </td></tr>
    <tr> <td> Article ID: </td><td> <input type=text name=article_id value=<? echo date("d_m_Y__H-i-s"); ?> 
    
    size=30> </td></tr>
    <tr> <td> Date: </td><td> <input type=text name=date value=<? echo date("d/m/Y\n"); ?> size=30 
    
    ></td></tr>
    <tr> <td> Summary: </td><td> <textarea name=summary 
    
    onKeyDown="countAreaChars(this.form.feedback,this.form.leftChars,150);"
    onKeyUp="countAreaChars(this.form.summary,this.form.leftChars,150);" rows=5 cols=30 
    
    wrap=soft></textarea>
    
    
    
    <input type=text name=leftChars readonly size=3 maxlength=3 value="150"> Letters Remaining
    
    
     </td></tr>
    <tr> <td> Body: </td><td> <textarea name=body rows=15 cols=30></textarea> </td></tr>
    
    <tr> <td> Picture: </td><td><input type=file name=userfile size=35 onChange="estensione(this.form, 
    
    this.value)"></td></tr>
    <tr> <td> Remember! All Pictures Uploaded
    MUST be JPG or GIF
    and have dimensions
    width: 450 px 
    
    height: 225 px  </td><td></td></tr> 
    </table>
    <input type=submit name=submit onclick="return controllo();" class="smallboldtext" value="Изпратете">
    <input type="reset" value="Откажете" name="Откажете" class="smallboldtext">
    </form>

    tutto sta nella riga di comando

    if ($userfile = "") { $add = "empty.jpg"; } else { $add = "$userfile_name"; }

    che ho evidenziato in rosso, NON FUNZIONA AL MOMENTO, ovvero se non inserisco un-immagine il file php mi restituisce il campo vuoto, come fare?

  2. #2
    Utente di HTML.it L'avatar di polinet
    Registrato dal
    Nov 2000
    Messaggi
    993
    Buttata giu al volo!!! provala
    Codice PHP:
    if  ($userfile == "") { $add "empty.jpg"; } else { $add "$userfile_name"; } 
    ¿Hasta la pasta?

  3. #3
    GRAZIE FUNZIONA ORA!

    avevo provato prima con il == ma non andava...ma ora si!!!

    chissa' perche' ..
    cmq ora va alla grande

    mi hai salvato la vita!

    a buon rendere!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.