I'm having troubles with uploading files. When I try to ftp, I get this error:
Warning: php_hostconnect: connect failed in /catchusers3/2012092/bradley/uploadpics.php on line 3
When using regular uploading, I get this error (only when I try to upload say more than 3)
STDERR:
Failed to find the "nothing" line (\n\n)! Premature end of script headers.
STDOUT:
(nothing)
Here's my code:
<?php
/* set up basic connection
$conn_id = ftp_connect("localhost");
// login with username and password
$login_result = ftp_login($conn_id, "site", "pword");
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
*/
foreach ($_FILES["userFile"]["error"] as $key => $error) {
$uploaddir = 'uploads/';
$file_name = basename($_FILES['userFile']['name'][$key]);
if($file_name != "")
{
$file_name = str_replace("\\","",$file_name);
$file_name = str_replace("'","",$file_name);
$file_type = $_FILES['userFile']['type'][$key];
$temp_name = $_FILES['userFile']['tmp_name'][$key];
$validate = validata($file_name, $file_type);
uploadFile($file_name, $temp_name, $uploaddir, $error, $validate);
addToMySQL($file_name, $validate);
}
}
function validata($fileName, $file_type)
{
$FILE_EXT = array('.jpg','.jpeg','.bmp','.gif','.png');
$FILE_MIMES=array('image/jpeg','image/jpg','image/gif','image/png','image/bmp');
$file_ext = strtolower(substr($fileName,strrpos($fileName,".")));
if (!in_array($file_type, $FILE_MIMES) && !in_array($file_ext, $FILE_EXT) ){
$fileMessage = "Sorry, $file_name($file_type) is not allowed to be uploaded.<br>";
echo "$fileMessage";
return (false);
}else{
return (true);
}
}
function addToMySQL($fileName, $valid)
{
$link = mysql_connect("localhost", "uname", "pwerd") or die("Couldn't connect to database");
mysql_select_db('dbname') or die('Could not select database');
if($valid == "TRUE")
{
$queryPK = "SELECT MAX(pk)FROM pics";
$recSetPK = mysql_query($queryPK, $link);
$rowPK = mysql_fetch_array($recSetPK);
if($rowPK[0] == ""){
$pk = 0;
}else{
$pk = $rowPK[0] + 1;
}
$queryPicNum = "SELECT MAX(picNum)FROM pics";
$recSetPicNum = mysql_query($queryPicNum, $link);
$rowPicNum = mysql_fetch_array($recSetPicNum);
if($rowPicNum[0] == ""){
$picNum = 0;
}else{
$picNum = $rowPicNum[0] + 1;
}
$queryInsert = "INSERT INTO pics VALUES($pk, '$fileName', $picNum)";
mysql_query($queryInsert, $link) or die("Could not insert into db!");
}
}
function uploadFile($fileName, $tempName, $uploadDir, $error, $valid)
{
if($valid == "TRUE")
{
if($error == UPLOAD_ERR_OK)
{
$file = $uploadDir.$fileName;
move_uploaded_file($tempName, $file);
$fileMessage .= "$file uploaded successfully<br>";
echo "$fileMessage";
}
}
}
?>
<html>
<body>
<?=$fileMessage?>
<a href="url">Back to upload files</a>
</body>
</html>
Thanks for any help you can give me. This problem seems like it's on the server side, but I'm no expert..