Ciao a tutti, sono assolutamente nuovo di php, devo completare alcune pagine per un progetto di solidarietà. Sto cercando di creare una pagina per un'associazione in cui l'utente registrato possa caricare una propria immagine ed eventualmente sovrascriverla in seguito, mi da un errore apparentemente molto comune ma cercando su forum e articoli non sono riuscito a risolvere... non so bene come lavorare con sessioni e header.
in locale il caricamento immagine funziona bene. In remoto, mi da l'errore sull'header. Il codice di seguito potrebbe contenere orrori. Vi sono infinitamente grato per qualsiasi suggerimento, da alcuni giorni non riesco a ottenere risultati.
riporto l'inizio del file..
e di seguito il div contenente la parte di upload immagine..
L'errore che mi da in remoto al caricamento di un'immagine è
Warning: Cannot modify header information - headers already sent by (output started at /home/web/www.sangiu9.com/www/temp_teatruck/partecipa.php:14) in /home/web/www.sangiu9.com/www/temp_teatruck/partecipa.php on line 219
e la riga incriminata è
dove faccio il refresh della pagina se il caricamento non da errori.
Grazie per il vostro tempo,
Matteo
in locale il caricamento immagine funziona bene. In remoto, mi da l'errore sull'header. Il codice di seguito potrebbe contenere orrori. Vi sono infinitamente grato per qualsiasi suggerimento, da alcuni giorni non riesco a ottenere risultati.
riporto l'inizio del file..
Codice:
<?php
session_start();
if(!isset($_SESSION['SESS_MEMBER_ID']) || (trim($_SESSION['SESS_MEMBER_ID']) == ''))
$ctr_sessione = "noses";
$s_login=$_SESSION['SESS_LOGIN'];
?>
<!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>
<title>Camion del the | Partecipa</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/cufon-yui.js"></script>
<script type="text/javascript" src="js/arial.js"></script>
<script type="text/javascript" src="js/cuf_run.js"></script>
</head>
<body>
<?php
if( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) >0 ) {
echo '<ul class="err">';
foreach($_SESSION['ERRMSG_ARR'] as $msg) {
echo '<li>',$msg,'</li>';
}
echo '</ul>';
unset($_SESSION['ERRMSG_ARR']);
}
?>
<div class="main">
<div class="header">
<div class="header_resize">
<div class="logo">
<h1><a href="index.php"><span>Camion</span> del The <br /><small> mettiamoci la faccia! </small></a></h1>
</div>
<div class="menu">
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="ilprogetto.php">Il progetto</a></li>
<li><a href="partecipa.php" class="active">Partecipa</a></li>
<li><a href="about.php">Chi Siamo</a></li>
<li><a href="contact.php">Contatti</a></li>
</ul>
</div>
<div class="clr"></div>
</div>
<div class="headert_text_resize_other">
e di seguito il div contenente la parte di upload immagine..
Codice:
<div class="body_resize">
<div class="left">
<div class="resize_bg">
<!-- if not login (significa che non ha pagato!!) -->
[... form di iscrizione...]
<!-- if login -> pagina utente -->
<?php
} else {
?>
<h2> Grazie per aver contribuito al progetto!</h2>
<p>
<br /> Seleziona di seguito l'immagine che desideri mandare in Tanzania con il camion del The!
</p>
<!-- ###################### -->
<!-- codice upload immagine -->
<!-- ###################### -->
<?php
session_start;
//Include database connection details
require_once('config.php');
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//define a maxim size for the uploaded images in Kb
define ("MAX_SIZE","100");
//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
//This variable is used as a flag. The value is initialized with 0 (meaning no error found)
//and it will be changed to 1 if an errro occures.
//If the error occures the file will not be uploaded.
$errors=0;
//checks if the form has been submitted
if(isset($_POST['Submit']))
{
//reads the name of the file the user submitted for uploading
$image=$_FILES['image']['name'];
//if it is not empty
if ($image)
{
//get the original name of the file from the clients machine
$filename = stripslashes($_FILES['image']['name']);
//get the extension of the file in a lower case format
$extension = getExtension($filename);
$extension = strtolower($extension);
//if it is not a known extension, we will suppose it is an error and will not upload the file,
//otherwise we will do more tests
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
//print error message
echo '<h1>Unknown extension!</h1>';
$errors=1;
}
else
{
//get the size of the image in bytes
//$_FILES['image']['tmp_name'] is the temporary filename of the file
//in which the uploaded file was stored on the server
$size=filesize($_FILES['image']['tmp_name']);
//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*1024)
{
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
}
//we will give an unique name, for example the time in unix time format
$image_name=$s_login.'.'.$extension;//time().'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname="uimages/".$image_name;
//we verify if the image has been uploaded, and print error instead
//Create INSERT query
$qry = "UPDATE users SET urlpic = '$newname' WHERE member_id = '".$_SESSION['SESS_MEMBER_ID']."'";
//echo $qry;
$result = @mysql_query($qry);
//Check whether the query was successful or not
if($result) {
$_SESSION['SESS_PIC_URL'] = $newname;
//header("location: register-success.php");
//exit();
}else {
die("Query failed");
}
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied)
{
echo '<h1>Copy unsuccessfull!</h1>';
$errors=1;
}}}}
//If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors)
{
//echo "<h1>File Uploaded Successfully! Try again!</h1>";
header("refresh:1;url=partecipa.php");// "location: partecipa.php");
//exit;
}
?>
<!--next comes the form, you must set the enctype to "multipart/frm-data" and use an input type "file" -->
<?php
session_start();
$test = $_SESSION['SESS_PIC_URL'];
if(!isset($_SESSION['SESS_PIC_URL']) || (trim($_SESSION['SESS_PIC_URL']) == ''))
//if(!isset($_SESSION['SESS_MEMBER_ID']))
{?>
<?php
} else {
//print($test);
?>
<table>
<tr><td><img src="<?php echo $test; ?>"></img></td></tr>
</table>
<?php
}
?>
<form name="newad" method="post" enctype="multipart/form-data" action="">
<table>
<tr><td><input type="file" name="image"></td></tr>
<tr><td><input name="Submit" type="submit" value="Upload image"></td></tr>
</table>
</form>
<?php
}
?>
</div>
</div>
L'errore che mi da in remoto al caricamento di un'immagine è
Warning: Cannot modify header information - headers already sent by (output started at /home/web/www.sangiu9.com/www/temp_teatruck/partecipa.php:14) in /home/web/www.sangiu9.com/www/temp_teatruck/partecipa.php on line 219
e la riga incriminata è
Codice:
//If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors)
{
//echo "<h1>File Uploaded Successfully! Try again!</h1>";
[COLOR="red"]header("refresh:1;url=partecipa.php");// "location: partecipa.php");[/COLOR]
//exit;
}
dove faccio il refresh della pagina se il caricamento non da errori.
Grazie per il vostro tempo,
Matteo